我正在查找树遍历,到目前为止,我没有理解它,这意味着预订,按顺序,后序。而简单的代码,如:树遍历,阅读代码
sub P(TreeNode)
Output(TreeNode.value)
If LeftPointer(TreeNode) != NULL Then
P(TreeNode.LeftNode)
If RightPointer(TreeNode) != NULL Then
P(TreeNode.RightNode)
end sub
我发现一个代码是更长,我感到困惑,会有什么输出?
public class My {
public static void print(Node n){
if(n != null) {
System.out.print(n.info +"");
print(n.left);
print(n.right);
}
}
public static void print2(Node n){
if(n != null) {
print2(n.left);
System.out.print(n.info +"");
print2(n.right);
}
}
public static void print3(Node n){
if(n != null) {
print3(n.left);
print3(n.right);
System.out.print(n.info +"");
}
}
public static void main(String[] args) {
Tree t = new Tree();
t.createTree();
print(t.root);
System.out.println();
print2(t.root);
System.out.println();
print3(t.root);
}
}
等只是它容易,我明白让说:输入是10,-10,12,8,21,34
什么将输出样子?因为如果我理解正确,那么所有3(预购,按序,后序)。
树的样子:
10
-10 12
8 21 34
原来,这是我的学校之一考试,他们必须给出答案一个纸,这是他们所得到的信息。没有人告诉他们树如何看。
我们看不到你的Tree或Node类,所以这可能不会打印任何东西......你应该在你的代码中运行你的代码一个调试器,虽然 –
@ cricket_007正确的,brainfart的时刻,对不起 –