Skip to content

Commit a1ff6ef

Browse files
committed
added problem 1361
1 parent 8cb3da8 commit a1ff6ef

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

problems/problem1361/validate_tree.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package problem1361
2+
3+
/*
4+
You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i],
5+
return true if and only if all the given nodes form exactly one valid binary tree.
6+
If node i has no left child then leftChild[i] will equal -1, similarly for the right child.
7+
Note that the nodes have no values and that we only use the node numbers in this problem.
8+
*/
9+
10+
func validateBinaryTreeNodes(n int, leftChild []int, rightChild []int) bool {
11+
// uf[i] represents the group that is the parent of group i
12+
var uf = make([]int, n)
13+
// indegree[i] represents how many nodes lead to node i
14+
var indegree = make([]int, n)
15+
16+
// Initialize the disjoint set (union find) group
17+
for i := range uf {
18+
uf[i] = i
19+
}
20+
21+
// Loop over the nodes and build the graph
22+
for i := 0; i < n; i++ {
23+
// If the is a child node
24+
if leftChild[i] != -1 {
25+
// Increment the node's indegree
26+
indegree[leftChild[i]]++
27+
// Update the parent graph
28+
union(uf, leftChild[i], i)
29+
}
30+
31+
// Same for right child
32+
if rightChild[i] != -1 {
33+
indegree[rightChild[i]]++
34+
union(uf, rightChild[i], i)
35+
}
36+
}
37+
38+
// There should be only 1 root (a node with no indegree)
39+
var foundRoot = false
40+
for i := range indegree {
41+
if indegree[i] > 1 {
42+
return false
43+
}
44+
if indegree[i] == 0 {
45+
if foundRoot {
46+
return false
47+
}
48+
foundRoot = true
49+
}
50+
}
51+
52+
// Find how many different groups there are
53+
var uniqGroups = map[int]bool{}
54+
for i := range uf {
55+
uniqGroups[find(uf, i)] = true
56+
}
57+
58+
// There should be only one group and one root
59+
return len(uniqGroups) == 1 && foundRoot
60+
}
61+
62+
func find(uf []int, x int) int {
63+
if uf[x] == x {
64+
// If x is the parent of itself, it is the root of the group
65+
return uf[x]
66+
} else {
67+
// If x is not the parent of itself, we call this function again
68+
// to find the real parent, and update the map
69+
uf[x] = find(uf, uf[x])
70+
return uf[x]
71+
}
72+
}
73+
74+
func union(uf []int, x, y int) {
75+
var rootx, rooty int
76+
// Finding the roots of x and y
77+
rootx = find(uf, x)
78+
rooty = find(uf, y)
79+
// Setting the root of rootx be rooty effectivly merging the groups
80+
uf[rootx] = rooty
81+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem1361
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
N int
12+
Left, Right []int
13+
Expected bool
14+
}
15+
16+
var TestCases = []TestCase{
17+
//{4, []int{1, -1, 3, -1}, []int{2, -1, -1, -1}, true},
18+
//{4, []int{1, -1, 3, -1}, []int{2, 3, -1, -1}, false},
19+
//{2, []int{1, 0}, []int{-1, -1}, false},
20+
{4, []int{1, 2, 0, -1}, []int{-1, -1, -1, -1}, false},
21+
}
22+
23+
func TestValidateBinaryTree(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
for _, tc := range TestCases {
27+
want := tc.Expected
28+
got := validateBinaryTreeNodes(tc.N, tc.Left, tc.Right)
29+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
30+
}
31+
}
32+
33+
func BenchmarkValidateBinaryTree(b *testing.B) {
34+
for _, tc := range TestCases {
35+
for i := 0; i < b.N; i++ {
36+
validateBinaryTreeNodes(tc.N, tc.Left, tc.Right)
37+
}
38+
}
39+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ Each problem is in it's own directory, with test files. There are helper package
440440
| 1351 | [Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix) | [My Solution](./problems/problem1351) ||
441441
| 1354 | [Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums) | [My Solution](./problems/problem1354) ||
442442
| 1357 | [Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders) | [My Solution](./problems/problem1357) ||
443+
| 1361 | [Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes) | [My Solution](./problems/problem1361) ||
443444
| 1372 | [Longest ZigZag Path in a Binary Tree](https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree) | [My Solution](./problems/problem1372) ||
444445
| 1376 | [Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees) | [My Solution](./problems/problem1376) ||
445446
| 1379 | [Find a Binary Tree Node in a Clone of That Tree*](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree) | [My Solution](./problems/problem1379) ||

0 commit comments

Comments
 (0)