Prove safety offer: a binary tree, and all the paths to a certain value (java)

Title: input an integer and a binary tree, the binary tree paths prints out all of the nodes and the value for the input integer. Forming a path from the root node down the tree until all the nodes through which the leaf node.

Below, and the integer input binary tree 22, print the two paths, a first path comprising nodes 10, 12, the second path comprises a node is 10,5,7.


     Since the path from the root to leaf node, that is the starting point of the path is always the root bit, so we first need to traverse the root, which is the binary tree traversal according to the preamble, the preamble from the figure traversing the point of view, after the access node 10, will access node 5. As can be seen from the definition of the binary tree nodes, the node in the binary tree to this question is not a pointer to a parent node, the access time to the node 5, we do not know what passes in front of the node, unless we put nodes on the path through which are stored. Each access to a node, we regard the current node is added to the route to go. Node 5 is reached, the path comprises two nodes, their values ​​are 10 and the next traversal to node 5. 4, we add this node to the path, this time has reached a leaf node , but the values ​​of the three nodes on the path and not equal to the input 19. the value 22, and therefore do not meet the requirements of the path.

    We then traverse to the other nodes. In traversing a node before, have to return to the node 4 from the node 5, go to the right child node traversal point 5 7. It is noteworthy that, when returned to the node 5, since the node 4 has not Go to junction 7 of the path, we need to delete the node 4 from the path. Next, access to the node 7 the time, then the node is added to the path. At this time, the path of three nodes 10,5,7 and exactly 22, is a path that complies with the requirements.

    Finally, we have to traverse a node 12. The node before the traverse, via a node 5 need to Back to node 10. Similarly, every time back from a child node of the parent node, we need on the path Removes the child node. Finally, the arrival node 10 from the node 12 when the two nodes on the path is the sum of 22, so this is in line with a path condition.

    After completion of specific examples of the previous analysis, we found a number of laws. When preorder before use to access a node, we add the node to the path, and the accumulated value of the node. If the node is a leaf node and the path node value and just an integer input, the current path to meet the requirements, we print it out. If the current node is not a leaf node, then continue to access its child nodes. After the end of the current node access, recursive function will automatically return to its parent node. So we want to remove before the function exits on the path to the current node and subtracting the value of the current node, just to make sure that the path is the path from the root to the parent when the parent returns. We can easily see the data structure stored path is actually a stack, so the path to a consistent state with the recursive call, but in essence is a process of recursive call stack and push out.

 
 public void findPath(BinaryTreeNode root,int k){  
        if(root == null)  
            return;  
        Stack<Integer> stack = new Stack<Integer>();  
        findPath(root,k,stack);  
    }  
    public void findPath(BinaryTreeNode root,int k,Stack<Integer> path){  
        if(root == null)  
            return;  
        if(root.leftNode == null && root.rightNode == null){  
            if(root.value == k){  
                System.out.println("路径开始");  
                for(int i :path)  
                    System.out.print(i+",");  
                System.out.print(root.value);  
            }  
        }  
        else{  
            path.push(root.value);  
            findPath(root.leftNode,k-root.value,path);  
            findPath(root.rightNode,k-root.value,path);  
            path.pop();  
        }  
    }  


Published 118 original articles · won praise 35 · views 120 000 +

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52729315