- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Construct BST from given preorder traversal - Set 2 in C++
Suppose we have one pre order traversal. From this traversal. we have to generate the tree So if the traversal is like [10, 5, 1, 7, 40, 50], then the tree will be like −
To solve this, we will follow these steps −
Create empty stack
Make the first value as root, and push it into stack.
Now keep pooping while the stack is not empty and the next value is greater than stack top element, make this as right child of the last popped node. Now push the new node to the stack.
When the next value is less than the top element, make it as left child of stack top element. Now push the new node into the stack.
Repeat the steps 2 and 3 until we have checked all of the preorder list elements.
Example
#include <iostream> #include <stack> using namespace std; class node { public: int data; node *left; node *right; }; node* getNode (int data) { node* temp = new node(); temp->data = data; temp->left = temp->right = NULL; return temp; } node* constructTree ( int pre[], int size ) { stack<node*> stk; node* root = getNode( pre[0] ); stk.push(root); int i; node* temp; for ( i = 1; i < size; ++i ) { temp = NULL; while ( !stk.empty() && pre[i] > stk.top()->data ) { temp = stk.top(); stk.pop(); } if ( temp != NULL) { temp->right = getNode( pre[i] ); stk.push(temp->right); } else { node* peek_node = stk.top(); peek_node->left = getNode( pre[i] ); stk.push(stk.top()->left); } } return root; } void inord (node* node) { if (node == NULL) return; inord(node->left); cout << node->data << " "; inord(node->right); } int main () { int pre[] = {10, 5, 1, 7, 40, 50}; int size = sizeof( pre ) / sizeof( pre[0] ); node *root = constructTree(pre, size); cout << "Inorder traversal: "; inord(root); }
Output
Inorder traversal: 1 5 7 10 40 50
- Related Articles
- Construct BST from given preorder traversal - Set 1 in C++
- Find postorder traversal of BST from preorder traversal in C++
- Construct BST from its given level order traversal in C++
- Construct a BST from given postorder traversal using Stack in Python
- Construct Binary Search Tree from Preorder Traversal in Python
- Construct the full k-ary tree from its preorder traversal in C++
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- Construct Tree from given Inorder and Preorder traversals in C++
- Print Postorder traversal from given Inorder and Preorder traversals
- Number of elements smaller than root using preorder traversal of a BST in C++
- Recover a Tree From Preorder Traversal in C++
- Construct Special Binary Tree from given Inorder traversal in C++
- N-ary Tree Preorder Traversal in C++
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree

Advertisements