
Asked in Josh Technology Group
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 Software Developer Interview Questions Asked at Josh Technology Group
Q. Given the head of a linked list, determine if the linked list has a cycle in it....read more
Q. Given a binary tree, imagine yourself standing on the left side of it, return th...read more
Q. What is the software development life cycle, and what steps does it involve?
Interview Questions Asked to Software Developer at Other Companies
Top Skill-Based Questions for Josh Technology Group Software Developer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 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

