Title: Enter a binary tree and an integer to print out all paths for the value of the node in the two-fork tree and for the input integer. A path is formed from the root node of the tree down to all the nodes through which the leaf nodes pass.
For example, input binary tree and integer 22, then print out two paths, the first path contains nodes 10, 12, the second path contains a node of 10,5,7.
The general data structure and the algorithm of the textbook do not introduce the path of the tree, so for most candidates, this is a new concept, it is difficult to come up with a complete problem-solving ideas. This time we can try to start with one or two specific examples to find patterns.
Taking the two-fork tree in the figure as an example, the path is from the root node to the leaf node, that is, the path is the root node as the starting point, so I first need to traverse the root node. In the pre-order, middle sequence and post-order three kinds of traversal methods, only the pre-sequence traversal is the first access to the root node.
Traverse the two-fork tree in the order of the preceding sequence traversal, after accessing node 10, Will access the node 5. From the definition of the two-fork tree node, it can be seen that there is no pointer to the parent node in the two-tree node of the subject, and when accessing node 5, we do not know which nodes are in the front, unless we save the nodes that pass through the path. Every time we visit a node, we add the current node to the path. At the junction 5 o'clock, the path contains two nodes, their values are 10 and 5, respectively. Next, we'll go to Node 4, and we'll add the node to the path, which has reached the leaf node, but the sum of the three nodes on the path is 19. This and does not equal the value entered by 22, and therefore does not conform to the required path.
We then go through the other nodes. Before traversing the next node, go back from Node 4 to node 5, and then Traverse Node 5 right child 7. It is worth noting that when we go back to node 5, we need to remove node 4 from the path because node 7 is not on the path to node 4. Next, when you access node 7, add the node to the path. The 10,5,7 of the three nodes in the path is exactly 22, which is a path that meets the requirements.
The last node we want to traverse is 12. Before traversing this node, it is necessary to go through node 5 to node 10. Again, every time we go back from a child node to a parent, we need to delete the child nodes on the path. Finally, from node 10 to node 12, the sum of two nodes on the path is also 22, so this is also a qualifying path.
The summary process is as follows:
After analyzing the specific examples above, we found some rules. When accessing a node in the way of a pre-order traversal, we add it to the path and accumulate the value of that junction. If the node is a leaf node and the value of the node in the path is exactly the input integer, then the current path meets the requirements and we print it out. If the current node is not a leaf node, continue to access its child nodes. At the end of the current node access, the recursive function automatically returns to its parent node. So we want to delete the current node on the path and subtract the value of the current node before the function exits to ensure that the path is exactly the path from the root node to the parent node when the parent node is returned. It is not difficult to see that the data structure of the saved path is actually a stack, so the path is consistent with the recursive invocation state, and the recursive invocation is essentially a stack-and-stack process.
Implemented in Java code:
/** * Enter a binary tree and an integer to print out all paths for the value of the node in the two-fork tree and for the input integer. A path is formed from the root node of the tree down to the node through which the leaf nodes pass. */package swordforoffer;import Java.util.stack;import Utils. binarytreenode;/** * @author Jinshuangqi * * August 5, 2015 */public class E25pathintree {public void Findpath (Binarytreenode r Oot,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 ("path start"); for (int i:p ath) 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 );p Ath.pop ();}} public static void Main (string[] args) {Binarytreenode root1 = new Binarytreenode (); Binarytreenode Node1 = new Binarytreenode (); Binarytreenode Node2 = new Binarytreenode (); Binarytreenode node3 = new Binarytreenode (); Binarytreenode NoDE4 = new Binarytreenode (); Binarytreenode node5 = new Binarytreenode (); Binarytreenode node6 = new Binarytreenode (); root1.leftnode = Node1;root1.rightnode = Node2;node1.leftnode = Node3; Node1.rightnode = Node4;node4.leftnode = Node5;node4.rightnode = Node6;root1.value = 8;node1.value = 8;node2.value = 7;nod E3.value = 9;node4.value = 2;node5.value = 4;node6.value = 7; E25pathintree test = new E25pathintree (); Test.findpath (ROOT1, 25);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point of Offer (Java Edition): Two fork in tree and path to a value