- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Advanced
- C++ Files and Streams
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
Construct Tree from given Inorder and Preorder traversals in C++
We are given the Inorder and Preorder traversals of a binary tree. The goal is to construct a tree from given traversals.
Inorder traversal − In this type of tree traversal, a left subtree is visited first, followed by the node and right subtree in the end.
Inorder (tree root)
Traverse left subtree of node pointed by root, call inorder ( root→left )
Visit the root
Traverse right subtree of node pointed by root, call inorder ( root→right )
Preorder traversal − In this type of tree traversal, the node visited first, followed by the left subtree and right subtree in the end.
Preorder (tree root)
- Visit the root
- Traverse left subtree of node pointed by root, call inorder ( root→left )
- Traverse right subtree of node pointed by root, call inorder ( root→right )
The inorder and preorder traversal of below tree are given −
Now we’ll construct the above tree again for given preorder and inorder traversals.
As we know that preorder visits the root node first then the first value always represents the root of the tree. From above sequence 5 is the root of the tree.
From above inorder traversal, we know that a node’s left subtree is traversed before it followed by its right subtree. Therefore, all values to the left of 5 in inorder belong to its left subtree and all values to the right belong to its right subtree.
2-3-4 ← 5 → 6-8-10
- Now for the left subtree do the same as above.
Preorder traversal of left subtree is 3 -2-4. So 3 becomes the root.
Inorder traversal divided further into 2 ← 3 → 4
- Now for the right subtree do the same as above.
Preorder traversal of the right subtree is 8 -6-10. So 8 becomes the root.
Inorder traversal divided further into 6 ← 8 → 10
So, in this way we constructed the original tree from given preorder and inorder traversals.
- Related Articles
- Print Postorder traversal from given Inorder and Preorder traversals
- Preorder from Inorder and Postorder traversals in C++
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Construct Special Binary Tree from given Inorder traversal in C++
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- Construct Binary Tree from Inorder and Postorder Traversal in Python
- Construct a Binary Tree from Postorder and Inorder in Python
- Construct Binary Search Tree from Preorder Traversal in Python
- Program to generate tree using preorder and inorder traversal in python
- Construct the full k-ary tree from its preorder traversal in C++
- Construct BST from given preorder traversal - Set 1 in C++
- Construct BST from given preorder traversal - Set 2 in C++
- Recover a Tree From Preorder Traversal in C++
- Construct a Binary Search Tree from given postorder in Python