Open In App

If you are given two traversal sequences, can you construct the binary tree?

Last Updated : 15 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

It depends on what traversals are given. If one of the traversal methods is Inorder then the tree can be constructed, otherwise not.

  Mirror 

Therefore, following combination can uniquely identify a tree.

  • Inorder and Preorder. 
  • Inorder and Postorder. 
  • Inorder and Level-order. 

And following do not. 

  • Postorder and Preorder. 
  • Preorder and Level-order. 
  • Postorder and Level-order. 

For example, Preorder, Level-order and Postorder traversals are same for the trees given in above diagram. 

  • Preorder Traversal = AB 
  • Postorder Traversal = BA 
  • Level-Order Traversal = AB 

So, even if three of them (Pre, Post and Level) are given, the tree can not be constructed.



Next Article
Practice Tags :

Similar Reads

Vertical Traversal of a Binary Tree
Given a Binary Tree, the task is to find its vertical traversal starting from the leftmost level to the rightmost level. If multiple nodes pass through a vertical line, th...
11 min read
Check if an array can be Preorder of a BST
Given an array of numbers, the task is to check if the given array can represent the preorder traversal of a Binary Search Tree. Examples: Input: pre[] = [40, 30, 35, 80,...
14 min read
Longest Increasing Subsequence (LIS)
Given an array arr[] of size n, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of...
15+ min read
Longest Common Subsequence (LCS)
Given two strings, s1 and s2, the task is to find the length of the Longest Common Subsequence. If there is no common subsequence, return 0. A subsequence is a string gene...
15+ min read
Longest Common Substring
Given two strings 's1' and 's2', find the length of the longest common substring. Example: Input: s1 = "GeeksforGeeks", s2 = "GeeksQuiz" Output : 5 Explanation:The longest...
13 min read
Unbounded Knapsack (Repetition of items allowed)
Given a knapsack weight, say capacity and a set of n items with certain value vali and weight wti, The task is to fill the knapsack in such a way that we can get the maxim...
15+ min read
Double Knapsack | Dynamic Programming
Given an array arr[] containing the weight of 'n' distinct items, and two knapsacks that can withstand capactiy1 and capacity2 weights, the task is to find the sum of the...
15+ min read
0/1 Knapsack Problem
Given n items where each item has some weight and profit associated with it and also given a bag with capacity W, [i.e., the bag can hold at most W weight in it]. The task...
15+ min read
Find Median from Running Data Stream
Given a data stream arr[] where integers are read sequentially, the task is to determine the median of the elements encountered so far after each new integer is read. Ther...
15+ min read
Construct Special Binary Tree from given Inorder traversal
Given Inorder Traversal of a Special Binary Tree in which the key of every node is greater than keys in left and right children, construct the Binary Tree and return root....
2 min read