亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

算法 - 如何不用遞歸 列出 樹(shù)(多叉) 中根節(jié)點(diǎn)到葉節(jié)點(diǎn)的所有路徑(Java)
天蓬老師
天蓬老師 2017-04-18 10:51:58
0
4
683

比如,對(duì)于下面這個(gè)二叉樹(shù),它所有的路徑為:

8 -> 3 -> 1

8 -> 2 -> 6 -> 4

8 -> 3 -> 6 -> 7

8 -> 10 -> 14 -> 13

怎么用Java去實(shí)現(xiàn)?

天蓬老師
天蓬老師

歡迎選擇我的課程,讓我們一起見(jiàn)證您的進(jìn)步~~

reply all(4)
阿神

If you don’t need recursion, then use depth first!
Use the stack, first push the root node into the stack, if the stack is not empty, then pop it out and output the median value of the current node, then push the right subtree onto the stack first, then push the left subtree onto the stack, and then Determine whether the stack is empty, loop... The steps are as follows:
1) First push the root node of the binary tree into the stack
2) Determine whether the stack is empty, if not, pop it out of the stack and output the pop tree The value of the node
3) The right subtree of the pop tree node is pushed onto the stack
4) The left subtree of the pop tree node is pushed onto the stack
5) Loop back to (2)
This is the one I saw before Method, I wonder if it can help the questioner?

public void depthOrderTraversal(){  
        if(root==null){  
            System.out.println("empty tree");  
            return;  
        }         
        ArrayDeque<TreeNode> stack=new ArrayDeque<TreeNode>();  
        stack.push(root);         
        while(stack.isEmpty()==false){  
            TreeNode node=stack.pop();  
            System.out.print(node.value+"    ");  
            if(node.right!=null){  
                stack.push(node.right);  
            }  
            if(node.left!=null){  
                stack.push(node.left);  
            }             
        }  
        System.out.print("\n");  
    }  
Ty80

Replace recursion with stack: https://zh.coursera.org/learn...

大家講道理

Depth first? . .

洪濤

Use breadth-first traversal, then store all the parent nodes of the node in the state, and output them after reaching the leaf node.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template