Skip to content

Latest commit

 

History

History
208 lines (175 loc) · 5.41 KB

File metadata and controls

208 lines (175 loc) · 5.41 KB

中文文档

Description

You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.

If node i has no left child then leftChild[i] will equal -1, similarly for the right child.

Note that the nodes have no values and that we only use the node numbers in this problem.

 

Example 1:

Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
Output: true

Example 2:

Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
Output: false

Example 3:

Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
Output: false

 

Constraints:

  • n == leftChild.length == rightChild.length
  • 1 <= n <= 104
  • -1 <= leftChild[i], rightChild[i] <= n - 1

Solutions

Union find.

Python3

class Solution:
    def validateBinaryTreeNodes(
        self, n: int, leftChild: List[int], rightChild: List[int]
    ) -> bool:
        p = list(range(n))
        vis = [False] * n

        def find(x):
            if p[x] != x:
                p[x] = find(p[x])
            return p[x]

        for i in range(n):
            l, r = leftChild[i], rightChild[i]
            if l != -1:
                if vis[l] or find(i) == find(l):
                    return False
                p[find(i)] = find(l)
                vis[l] = True
                n -= 1
            if r != -1:
                if vis[r] or find(i) == find(r):
                    return False
                p[find(i)] = find(r)
                vis[r] = True
                n -= 1
        return n == 1

Java

class Solution {
    private int[] p;

    public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
        p = new int[n];
        boolean[] vis = new boolean[n];
        for (int i = 0; i < n; ++i) {
            p[i] = i;
        }
        for (int i = 0, t = n; i < t; ++i) {
            int l = leftChild[i], r = rightChild[i];
            if (l != -1) {
                if (vis[l] || find(i) == find(l)) {
                    return false;
                }
                vis[l] = true;
                p[find(i)] = find(l);
                --n;
            }
            if (r != -1) {
                if (vis[r] || find(i) == find(r)) {
                    return false;
                }
                vis[r] = true;
                p[find(i)] = find(r);
                --n;
            }
        }
        return n == 1;
    }

    private int find(int x) {
        if (p[x] != x) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}

C++

class Solution {
public:
    vector<int> p;

    bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
        p.resize(n);
        for (int i = 0; i < n; ++i) p[i] = i;
        vector<bool> vis(n, false);
        for (int i = 0, t = n; i < t; ++i) {
            int l = leftChild[i], r = rightChild[i];
            if (l != -1) {
                if (vis[l] || find(i) == find(l)) return false;
                vis[l] = true;
                p[find(i)] = find(l);
                --n;
            }
            if (r != -1) {
                if (vis[r] || find(i) == find(r)) return false;
                vis[r] = true;
                p[find(i)] = find(r);
                --n;
            }
        }
        return n == 1;
    }

    int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
};

Go

var p []int

func validateBinaryTreeNodes(n int, leftChild []int, rightChild []int) bool {
	p = make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = i
	}
	vis := make([]bool, n)
	for i, t := 0, n; i < t; i++ {
		l, r := leftChild[i], rightChild[i]
		if l != -1 {
			if vis[l] || find(i) == find(l) {
				return false
			}
			vis[l] = true
			p[find(i)] = find(l)
			n--
		}
		if r != -1 {
			if vis[r] || find(i) == find(r) {
				return false
			}
			vis[r] = true
			p[find(i)] = find(r)
			n--
		}
	}
	return n == 1
}

func find(x int) int {
	if p[x] != x {
		p[x] = find(p[x])
	}
	return p[x]
}

...