先序遍历二叉树---递归代码---运行过程

需要遍历的简单二叉树

代码

private int flag = 1;
private void prePrintTree(BinaryTreeNode root) {
    if (root != null) {
        System.out.print("[" + root.value + "]");
        prePrintTree(root.leftNode);
        prePrintTree(root.rightNode);
    }
    System.out.print("<" + flag++ + ">");
    return;
}

private BinaryTreeNode constructTree1() {
    
    BinaryTreeNode root2 = new BinaryTreeNode(3, null, null);
    BinaryTreeNode root1 = new BinaryTreeNode(2, null, null);
    BinaryTreeNode root = new BinaryTreeNode(1, root1, root2);
    return root;
}
BinaryTreeNode root = constructTree1();
prePrintTree(root);

 

 程序执行结果

[1][2]<1><2><3>[3]<4><5><6><7>

 

详细过程


 

本节完......

posted @ 2019-03-18 09:32  xinglichao  阅读(410)  评论(0编辑  收藏  举报