先序遍歷二叉樹---遞歸代碼---運行過程

須要遍歷的簡單二叉樹spa

代碼3d

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);

 

 程序執行結果code

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

 

詳細過程blog


 

本節完......class

相關文章
相關標籤/搜索