Find all paths for a value in the binary tree

Source: Internet
Author: User

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


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.