Question: Find all paths for a value in the binary tree
Enter an integer and a binary tree. Access from the root node of the tree to all the nodes that the leaf node passes through to form a path. Print all paths equal to the input integer. For example, if you enter the integer 22 and the following binary tree 10/\ 5 12/\ 4 7, two paths are printed: 10, 12, 10, 5, and 7. Ideas
1. When accessing a node, add the value of the node to the current node and the variable, and push the node into the stack.
2. If the node is a leaf node and the current CurrentSum is equal to the expected expectedSum, the node value in the stack is printed as the required path.
3. If the node is not a leaf node, continue to access its left child node and Its right child node.
4. Delete the node. This includes subtracting the node value from the current and variable, and then popping up the node value from the stack. At this time, the parent node has been returned.
The stack in the program uses the vector in STL to simplify coding.
The specific findPath Function Code is as follows:
[Cpp]
Void findPath (BtreeNode * root, int fig, vector <int> path, int currentSum ){
CurrentSum + = root-> v;
Path. push_back (root-> v );
If (root-> left = NULL & root-> right = NULL & currentSum = deleetedsum ){
Vector <int>: iterator iter;
For (iter = path. begin (); iter! = Path. end (); iter ++)
Cout <* iter <"";
} Else {
If (root-> left! = NULL)
FindPath (root-> left, fig, path, currentSum );
If (root-> right! = NULL)
FindPath (root-> right, fig, path, currentSum );
}
CurrentSum-= root-> v;
Path. pop_back ();
}
Void findPath (BtreeNode * root, int fig, vector <int> path, int currentSum ){
CurrentSum + = root-> v;
Path. push_back (root-> v );
If (root-> left = NULL & root-> right = NULL & currentSum = deleetedsum ){
Vector <int>: iterator iter;
For (iter = path. begin (); iter! = Path. end (); iter ++)
Cout <* iter <"";
} Else {
If (root-> left! = NULL)
FindPath (root-> left, fig, path, currentSum );
If (root-> right! = NULL)
FindPath (root-> right, fig, path, currentSum );
}
CurrentSum-= root-> v;
Path. pop_back ();
}
All the code that has passed the test:
[Cpp] // printTreePathSum. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <vector>
# Include <iostream>
Using namespace std;
Struct BtreeNode {
Int v;
Struct BtreeNode * left;
Struct BtreeNode * right;
};
BtreeNode * insertNode (BtreeNode * root, int value ){
BtreeNode * ptr = root;
BtreeNode * tmpNode;
BtreeNode * newNode = (BtreeNode *) malloc (sizeof (struct BtreeNode ));
NewNode-> v = value;
NewNode-> left = NULL;
NewNode-> right = NULL;
If (ptr = NULL)
Return newNode;
While (ptr! = NULL ){
TmpNode = ptr;
If (ptr-> v <value)
Ptr = ptr-> right;
Else
Ptr = ptr-> left;
}
If (tmpNode-> v <value)
TmpNode-> right = newNode;
Else
TmpNode-> left = newNode;
Return root;
}
BtreeNode * buildTree (){
BtreeNode * root = NULL;
Int;
While (cin> ){
Root = insertNode (root, );
}
Return root;
}
Void findPath (BtreeNode * root, int fig, vector <int> path, int currentSum ){
CurrentSum + = root-> v;
Path. push_back (root-> v );
If (root-> left = NULL & root-> right = NULL & currentSum = deleetedsum ){
Vector <int>: iterator iter;
For (iter = path. begin (); iter! = Path. end (); iter ++)
Cout <* iter <"";
} Else {
If (root-> left! = NULL)
FindPath (root-> left, fig, path, currentSum );
If (root-> right! = NULL)
FindPath (root-> right, fig, path, currentSum );
}
CurrentSum-= root-> v;
Path. pop_back ();
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
BtreeNode * root;
Root = buildTree ();
Vector <int> path;
FindPath (root, 22, path, 0 );
System ("pause ");
Return 0;
}
// PrintTreePathSum. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <vector>
# Include <iostream>
Using namespace std;
Struct BtreeNode {
Int v;
Struct BtreeNode * left;
Struct BtreeNode * right;
};
BtreeNode * insertNode (BtreeNode * root, int value ){
BtreeNode * ptr = root;
BtreeNode * tmpNode;
BtreeNode * newNode = (BtreeNode *) malloc (sizeof (struct BtreeNode ));
NewNode-> v = value;
NewNode-> left = NULL;
NewNode-> right = NULL;
If (ptr = NULL)
Return newNode;
While (ptr! = NULL ){
TmpNode = ptr;
If (ptr-> v <value)
Ptr = ptr-> right;
Else
Ptr = ptr-> left;
}
If (tmpNode-> v <value)
TmpNode-> right = newNode;
Else
TmpNode-> left = newNode;
Return root;
}
BtreeNode * buildTree (){
BtreeNode * root = NULL;
Int;
While (cin> ){
Root = insertNode (root, );
}
Return root;
}
Void findPath (BtreeNode * root, int fig, vector <int> path, int currentSum ){
CurrentSum + = root-> v;
Path. push_back (root-> v );
If (root-> left = NULL & root-> right = NULL & currentSum = deleetedsum ){
Vector <int>: iterator iter;
For (iter = path. begin (); iter! = Path. end (); iter ++)
Cout <* iter <"";
} Else {
If (root-> left! = NULL)
FindPath (root-> left, fig, path, currentSum );
If (root-> right! = NULL)
FindPath (root-> right, fig, path, currentSum );
}
CurrentSum-= root-> v;
Path. pop_back ();
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
BtreeNode * root;
Root = buildTree ();
Vector <int> path;
FindPath (root, 22, path, 0 );
System ("pause ");
Return 0;
}