LeetCode 1361: Validate Binary Tree Nodes Solution

Master LeetCode problem 1361 (Validate Binary Tree Nodes), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation

To validate if the given nodes form exactly one valid binary tree, we can perform a depth-first search (DFS) traversal starting from the root node. During the traversal, we mark each visited node as visited and ensure that no node is visited more than once. Additionally, we keep track of the total number of visited nodes to ensure that all nodes are connected in a single tree structure.

Algorithm:

  1. Create a visited array to mark nodes as visited.
  2. Initialize a counter to keep track of the total number of visited nodes.
  3. Perform a DFS traversal starting from the root node (node 0).
  4. During traversal, mark each visited node as visited and increment the counter.
  5. If a node is visited more than once or the total number of visited nodes is not equal to n, return false.
  6. Otherwise, return true.

Time Complexity:

The time complexity of this approach is O(n) where n is the number of nodes in the binary tree.

Space Complexity:

The space complexity of this approach is O(n) for the visited array.

Solution Code

class Solution {
    public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
        boolean[] visited = new boolean[n];
        int count = 0;
        
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                count++;
                if (!dfs(i, leftChild, rightChild, visited)) {
                    return false;
                }
            }
        }
        
        return count == 1;
    }
    
    private boolean dfs(int node, int[] leftChild, int[] rightChild, boolean[] visited) {
        if (node == -1) {
            return true;
        }
        
        if (visited[node]) {
            return false;
        }
        
        visited[node] = true;
        
        return dfs(leftChild[node], leftChild, rightChild, visited) && dfs(rightChild[node], leftChild, rightChild, visited);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1361 (Validate Binary Tree Nodes)?

This page provides optimized solutions for LeetCode problem 1361 (Validate Binary Tree Nodes) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1361 (Validate Binary Tree Nodes)?

The time complexity for LeetCode 1361 (Validate Binary Tree Nodes) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1361 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1361 in Java, C++, or Python.

Back to LeetCode Solutions