Height of Binary Tree
You are provided with the Inorder and Level Order traversals of a Binary Tree composed of integers. Your goal is to determine the height of this Binary Tree without actually constructing it.
The height of a Binary Tree is defined as the number of edges on the longest path from the root node to any leaf node. If the tree consists of only a single node, the height is considered to be 0.
Input:
Input begins with an integer ‘T’ denoting the number of test cases. Each test case includes the following: 1. An integer ‘N’ indicating the number of nodes in the binary tree. 2. ‘N’ integers representing the Inorder traversal of the binary tree. 3. ‘N’ integers representing the Level Order traversal of the binary tree.
Output:
The height of the binary tree should be printed for each test case separately. Each height is printed on a new line.
Example:
Input:
T = 1
N = 3
Inorder Traversal = 2 1 3
Level Order Traversal = 1 2 3
Output:
1
Constraints:
- 1 <= T <= 100
- 1 <= N <= 3000
- 1 <= inorder[i] <= N
- 1 <= levelOrder[i] <= N
Note: You are not required to print anything as this has been managed; implement the function to return the result.
Be the first one to answer
Add answer anonymously...
Top PolicyBazaar Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in PolicyBazaar Software Developer
>
PolicyBazaar Software Developer Interview Questions
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