Microsoft and other data structures and algorithms interview question 4

Source: Internet
Author: User

Question 4

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.

Analysis: (for details, see)
This topic mainly deals with the non-recursive travel of Binary trees, so it is easy to think of using stacks as the node storage for data traversal. Each time the data is pushed into the stack, it determines whether it has reached the leaf node and the sum of elements in the stack.
If the given value is met and the preceding two conditions are met, the elements in the stack are printed as true. Continue to traverse.

The first is the construction of the tree. In order to facilitate the use of the breadth-first Insertion Algorithm, there is a lot to do with this question.
Then, the non-recursive travel of Binary Trees.

[Cpp]
# Include <iostream>
# Include <queue>
# Include <stack>
Using namespace std;
 
Typedef struct NodeBinaryTree
{
Int value;
NodeBinaryTree * nodeLeft;
NodeBinaryTree * nodeRight;
} NodeBinaryTree;
 
Class BinaryTree
{
Private:
NodeBinaryTree * root;
Stack <int> Path;
Public:
 
BinaryTree ();
//~ BSTree (); recursively delete all nodes.
NodeBinaryTree * Root () {return root ;}
Void addNodeBSTree (const int item );
Void inOrder (NodeBinaryTree * root );

Void inOrderStack (const int item );
};
 
BinaryTree: BinaryTree ()
{
Root = NULL;
}
 
Void BinaryTree: addNodeBSTree (const int item)
{
// When writing code in this place, I used the insert node with the breadth-first priority. I did not know how to write it.
// It has little to do with this question and can be ignored directly
If (NULL = root)
{

NodeBinaryTree * node2add = new NodeBinaryTree ();
Node2add-> value = item;
Node2add-> nodeLeft = NULL;
Node2add-> nodeRight = NULL;
Root = node2add;
}
Else
{
// Insert of nodes in the BinaryTree uses the breadth-first insert method. Therefore, we define a queue and directly use the queue in STL.
Using std: queue;
Queue <NodeBinaryTree *> aQueue;
NodeBinaryTree * pointer = root;
If (pointer)
AQueue. push (pointer );
While (! AQueue. empty ())
{
Pointer = aQueue. front ();
AQueue. pop ();
If (NULL! = Pointer-> nodeLeft & NULL! = Pointer-> nodeRight) {// Why is there a bracket in the book?
AQueue. push (pointer-> nodeLeft );
AQueue. push (pointer-> nodeRight );
}
Else if (NULL! = Pointer-> nodeLeft & NULL = pointer-> nodeRight)
{
// Locate the current vertex without the right subtree and insert the vertex to be added to the right subtree
NodeBinaryTree * node2add = new NodeBinaryTree ();
Pointer-> nodeRight = node2add;
Node2add-> value = item;
Node2add-> nodeLeft = NULL;
Node2add-> nodeRight = NULL;
Break;
}
Else // It indicates that the Left subtree is NULL.
{
NodeBinaryTree * node2add = new NodeBinaryTree ();
Pointer-> nodeLeft = node2add;
Node2add-> value = item;
Node2add-> nodeLeft = NULL;
Node2add-> nodeRight = NULL;
Break;
}
 

}
}
 
}
 
Void BinaryTree: inOrder (NodeBinaryTree * root)
{
If (NULL! = Root)
{
InOrder (root-> nodeLeft );
Cout <root-> value <"";
InOrder (root-> nodeRight );
}
}
 
Void BinaryTree: inOrderStack (const int item)
{
Cout <"the expected sum of the path is" <item <endl;
NodeBinaryTree * root = this-> root;
// Non-recursive travel Binary Tree
Enum Tags {Left, Right };
Struct StackElement
{
NodeBinaryTree * pointer;
Tags tag;
};
 
StackElement element;
StackElement elementCopy;
Stack <StackElement> aStack;
Stack <StackElement> aCopyStack;
NodeBinaryTree * pointer;

If (NULL = root) {return ;}
Else pointer = root;
 
Int sumValue = 0;
Int nodeValue;
 
 
While (true ){
While (NULL! = Pointer)
{
Element. pointer = pointer;
Element. tag = Left;
AStack. push (element );
SumValue = sumValue + element. pointer-> value;
Pointer = pointer-> nodeLeft;
}
 
Element = aStack. top ();
If (element. tag = Right & NULL = element. pointer-> nodeLeft & NULL = element. pointer-> nodeRight & item = sumValue)
{
// Because right and left are taken into account each time the Tag is considered, the element. tag = Right condition is not used to print twice.
// If the path is found
// Output the elements in the stack and re-organize the stack to the original form.
While (! AStack. empty ()){
ElementCopy = aStack. top ();
AStack. pop ();
ACopyStack. push (elementCopy );
}
While (! ACopyStack. empty ()){
ElementCopy = aCopyStack. top ();
ACopyStack. pop ();
AStack. push (elementCopy );
Cout <elementCopy. pointer-> value <"";
}
Cout <endl;
 
}
 
AStack. pop ();
SumValue = sumValue-element. pointer-> value;
Pointer = element. pointer;
 
 

// Return from the right subtree
While (element. tag = Right ){

If (aStack. empty () return;
Else {
Element = aStack. top ();
AStack. pop ();
SumValue = sumValue-element. pointer-> value;
Pointer = element. pointer;
}

}
 
// Return from left subtree
Element. tag = Right;
AStack. push (element );
SumValue = sumValue + element. pointer-> value;
Pointer = pointer-> nodeRight;

}
 
 
}
 
 
Int main ()
{

BinaryTree;
A. addNodeBSTree (1 );
A. addNodeBSTree (2 );
A. addNodeBSTree (3 );
A. addNodeBSTree (4 );
A. addNodeBSTree (6 );
A. addNodeBSTree (5 );
A. addNodeBSTree (7 );
 
NodeBinaryTree * head = a. Root ();
// Test inorder
A. inOrder (head );
Cout <endl;
A. inOrderStack (9 );
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.