All paths in the binary tree and for a specific value (I see many answers on the Internet are wrong)

Source: Internet
Author: User

Question: 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, enter the integer 22 and the following Binary Tree

10
/\
5 12
/\
4 7

The following two paths are printed: 10, 12, 10, 5, and 7.

 

My thinking seems a little complicated. I use a vector and stack to store data, a vector output path, and a stack to store the path. Because stack output requires all output from the stack, data will be lost.

Some online answers, including those written by some great gods, have some problems. If the number of nodes is 1, a bug occurs. I suddenly wanted to correct the bug, so I wrote the following program.

 

 

[Cpp]
# Include <iostream>
# Include <vector>
# Include <stack>
Using namespace std;
// Node
Struct node
{
Node * lchild, * rchild;
Int value;
};
 
// Binary Search Tree
Class list
{
Public:
List ();
// Here the m_print function is used for recursion. The print function is equivalent to a set
Void print (int num );
Private:
// Privatize recursive functions
Void m_print (node * p, int value, int num, bool flag );
Private:
Node * root;
Vector <int> v;
Stack <node *> s;
};
Void list: m_print (node * p, int value, int num, bool flag)
{
If (p = NULL)
Return;
Value + = p-> value;
V. push_back (p-> value );
S. push (p );
// Return to the leaf node
If (NULL = p-> lchild & NULL = p-> rchild ){
If (value = num ){
For (vector <int >:: iterator iter = v. begin (); iter! = V. end (); iter ++)
Cout <* iter <"";
Cout <endl;
}
V. pop_back ();
S. pop ();
If (flag = 1 ){
V. pop_back ();
S. pop ();
}
While (1 ){
If (! S. empty ()&&! S. top ()-> rchild ){
S. pop ();
V. pop_back ();
}
Else
Break;
}
Return;
}
// Recursion in the left and right directions
M_print (p-> lchild, value, num, 0 );
M_print (p-> rchild, value, num, 1 );
 
}
 
 
Void list: print (int num)
{
V. clear ();
M_print (root, 0, num, 0 );
}
 
List: list ()
{
Cout <"Enter the node you want to enter and press '#' to exit:" <endl;
Int I;
// You can use cin. fail () and cin. bad () to determine whether or not
If (cin> I = NULL ){
Cout <"your input is incorrect" <endl;
Exit (-1 );
}
// Create the root node
Root = new node;
Root-> value = I;
Root-> lchild = NULL;
Root-> rchild = NULL;
// Create two temporary nodes, p open up new nodes, and q is binary search and positioning
Node * p, * q;
While (cin> I! = NULL ){
// Create a new node
P = new node;
P-> value = I;
P-> lchild = NULL;
P-> rchild = NULL;
// The binary search tree starts from q = root. If it is small, it turns to left, and if it is large, it turns to right. If it has a position, it is inserted.
Q = root;
While (1 ){
// The inserted node is smaller than the Compare value of the node. If the left node is empty, the inserted node is inserted. Otherwise, q = q-> lchild continues to judge.
If (p-> value <q-> value ){
If (q-> lchild)
Q = q-> lchild;
Else {
Q-> lchild = p;
Break;
}
}
// The inserted node is greater than the Compare value of the node. If the right node is empty, the inserted node is inserted. Otherwise, q = q-> rchild continues to judge.
Else if (p-> value> q-> value ){
If (q-> rchild)
Q = q-> rchild;
Else {
Q-> rchild = p;
Break;
}
}
// If the two nodes are equal, exit the program directly (somewhat violent)
Else {
Cout <"node repeated !! "<Endl;
Exit (-1 );
}
}
}
}
 
Void main ()
{
List test;
// The print function prints the length of all root nodes to all leaf nodes.
Test. print (22 );
System ("pause ");
}

# Include <iostream>
# Include <vector>
# Include <stack>
Using namespace std;
// Node
Struct node
{
Node * lchild, * rchild;
Int value;
};

// Binary Search Tree
Class list
{
Public:
List ();
// Here the m_print function is used for recursion. The print function is equivalent to a set
Void print (int num );
Private:
// Privatize recursive functions
Void m_print (node * p, int value, int num, bool flag );
Private:
Node * root;
Vector <int> v;
Stack <node *> s;
};
Void list: m_print (node * p, int value, int num, bool flag)
{
If (p = NULL)
Return;
Value + = p-> value;
V. push_back (p-> value );
S. push (p );
// Return to the leaf node
If (NULL = p-> lchild & NULL = p-> rchild ){
If (value = num ){
For (vector <int >:: iterator iter = v. begin (); iter! = V. end (); iter ++)
Cout <* iter <"";
Cout <endl;
}
V. pop_back ();
S. pop ();
If (flag = 1 ){
V. pop_back ();
S. pop ();
}
While (1 ){
If (! S. empty ()&&! S. top ()-> rchild ){
S. pop ();
V. pop_back ();
}
Else
Break;
}
Return;
}
// Recursion in the left and right directions
M_print (p-> lchild, value, num, 0 );
M_print (p-> rchild, value, num, 1 );

}


Void list: print (int num)
{
V. clear ();
M_print (root, 0, num, 0 );
}

List: list ()
{
Cout <"Enter the node you want to enter and press '#' to exit:" <endl;
Int I;
// You can use cin. fail () and cin. bad () to determine whether or not
If (cin> I = NULL ){
Cout <"your input is incorrect" <endl;
Exit (-1 );
}
// Create the root node
Root = new node;
Root-> value = I;
Root-> lchild = NULL;
Root-> rchild = NULL;
// Create two temporary nodes, p open up new nodes, and q is binary search and positioning
Node * p, * q;
While (cin> I! = NULL ){
// Create a new node
P = new node;
P-> value = I;
P-> lchild = NULL;
P-> rchild = NULL;
// The binary search tree starts from q = root. If it is small, it turns to left, and if it is large, it turns to right. If it has a position, it is inserted.
Q = root;
While (1 ){
// The inserted node is smaller than the Compare value of the node. If the left node is empty, the inserted node is inserted. Otherwise, q = q-> lchild continues to judge.
If (p-> value <q-> value ){
If (q-> lchild)
Q = q-> lchild;
Else {
Q-> lchild = p;
Break;
}
}
// The inserted node is greater than the Compare value of the node. If the right node is empty, the inserted node is inserted. Otherwise, q = q-> rchild continues to judge.
Else if (p-> value> q-> value ){
If (q-> rchild)
Q = q-> rchild;
Else {
Q-> rchild = p;
Break;
}
}
// If the two nodes are equal, exit the program directly (somewhat violent)
Else {
Cout <"node repeated !! "<Endl;
Exit (-1 );
}
}
}
}

Void main ()
{
List test;
// The print function prints the length of all root nodes to all leaf nodes.
Test. print (22 );
System ("pause ");
}

 

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.