Validate Binary Tree Nodes Problem
You are provided with 'N' binary tree nodes labeled from 0 to N-1, where node i has two potential children, recorded in the LEFT_CHILD[i]
and RIGHT_CHILD[i]
arrays. Determine whether all the nodes given constitute precisely one valid binary tree. If node i lacks a left child, then LEFT_CHILD[i]
will be -1; the same applies to the right child.
Example:
Input:
n = 4
LEFT_CHILD = [1, -1, 3, -1]
RIGHT_CHILD = [2, -1, -1, -1]
Output:
True
Explanation:
The structure forms a single valid binary tree with each node having only one parent and only one root.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 104
LEFT_CHILD.length == RIGHT_CHILD.length == N
-1 <= LEFT_CHILD[i], RIGHT_CHILD[i] <= N - 1
- Time Limit: 1 sec
Input:
The first line contains an integer ‘T’ denoting the number of test cases.
For each test case, the first line contains an integer ‘N’.
The next two lines for each test case provide the ‘LEFT_CHILD’ and ‘RIGHT_CHILD’ arrays.
Output:
For each test case, return ‘Yes’ or ‘No’ to indicate whether the nodes form precisely one valid binary tree.
Note:
No printing required; implement the function to return 'True' or 'False'. The nodes lack values; only node numbers are used in this context.
AnswerBot
1y
The task is to determine if the given binary tree nodes form exactly one valid binary tree.
Check if there is only one root node (a node with no parent)
Check if each node has at most one parent
Check if...read more
Help your peers!
Add answer anonymously...
Top Josh Technology Group Software Developer interview questions & answers
Popular interview questions of Software Developer
>
Josh Technology Group 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