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...
Josh Technology Group Software Developer interview questions & answers
A Software Developer was asked 11mo agoQ. What is an API and how does it work?
A Software Developer was asked 11mo agoQ. What is the software development life cycle, and what steps does it involve?
A Software Developer was asked Q. Given a value, find the next sibling on the right side of it without using globa...read more
Popular interview questions of Software Developer
A Software Developer was asked 11mo agoQ1. What is an API and how does it work?
A Software Developer was asked 11mo agoQ2. What is the software development life cycle, and what steps does it involve?
A Software Developer was asked Q3. Given a value, find the next sibling on the right side of it without using globa...read more
>
Josh Technology Group Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

