【发布时间】:2021-04-07 19:24:49
【问题描述】:
我还是 Java 的初学者。我刚刚了解了二叉搜索树和前序遍历的概念,以及如何使用递归来实现二叉树的前序遍历。像这样的:
Void preorder(node root){
if(root==null){//base case when node becomes null we stop recursive function
return ;}
System.out.print(root.data+" ");// printing the value of node as first time we visit
Preorder(root.left);// look at left side of tree ,once left side complete
Preorder(root.right);// start having a look at right side of that specific node
}
但是,如何为 N ary 树实现相同的递归模型?每个节点的子节点数不一定是 2?因为那么 .left 和 .right 将不适用,不是吗?如果需要提供更多代码,请lmk,谢谢。
【问题讨论】: