Binary Tree Traversals Problem Statement
Given a Binary Tree with 'N' nodes, where each node holds an integer value, your task is to compute the In-Order, Pre-Order, and Post-Order traversals of the binary tree.
Input:
The first line of input contains an integer 'T', indicating the number of test cases. For each test case, the first line consists of tree elements in level-order format, separated by a space. A value of -1 denotes a null node.
Output:
For each test case, return a list of lists containing three lists that represent the In-Order, Pre-Order, and Post-Order traversals respectively, each on a new line.
Example:
Input:
1 3 8 5 2 7 -1 -1 -1 -1 -1 -1 -1
Output:
5 3 2 1 7 4 6
1 3 5 2 4 7 6
5 2 3 7 6 4 1
Explanation:
The tree is structured as:
- In-Order traversal: 5, 3, 2, 1, 7, 4, 6
- Pre-Order traversal: 1, 3, 5, 2, 4, 7, 6
- Post-Order traversal: 5, 2, 3, 7, 6, 4, 1
Constraints:
1 ≤ T ≤ 100
0 ≤ N ≤ 3000
0 ≤ data ≤ 10^9
where 'data' is the node value of the binary tree nodes.- Time limit: 1 sec
Note:
- Parent-child relationships are deduced from the level order input syntax.
- The input ends when all nodes at the last level are null (-1).
- You do not need to print any results; just implement the function to compute the required traversals.
Be the first one to answer
Add answer anonymously...
Top BigBasket Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app