【发布时间】:2020-12-07 10:25:02
【问题描述】:
在网络上有打印值的解决方案,如下所示:
void printPostorder(Node node)
{
if (node == null)
return;
// first recur on left subtree
printPostorder(node.left);
// then recur on right subtree
printPostorder(node.right);
// now deal with the node
System.out.print(node.key + " ");
}
但我的问题是我不想打印这些值,而是将它们放在ArrayList 中。这部分很简单我尝试使用ArrayList.add 而不是System.out.print,但我的困难是我想返回它,所以我的返回类型将是void 而不是ArrayList。问题是我不知道在基本情况下要返回什么:
if (node == null)
return;
我的方法确实返回了 ArrayList,那么对于上述基本情况,我可以返回什么?
【问题讨论】: