-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathValidateBinaryTreeNodes.java
122 lines (115 loc) · 3.89 KB
/
ValidateBinaryTreeNodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*https://leetcode.com/problems/validate-binary-tree-nodes/*/
/*Normal Tree Based Solution*/
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int d)
{
data = d;
}
}
class Solution {
boolean result;
boolean[] visited;
public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
HashMap<Integer,TreeNode> treeMap = new HashMap<Integer,TreeNode>();
HashSet<Integer> possibleRoots = new HashSet<Integer>();
int i;
result = true;
visited = new boolean[n];
for (i = 0; i < n; ++i) //for each node
{
possibleRoots.add(i); //add to possible roots
treeMap.put(i,new TreeNode(i)); //add a node in the map
}
for (i = 0; i < n; ++i) //for each node
{
if (leftChild[i] != -1) //if left child is present
{
((TreeNode)treeMap.get(i)).left = (TreeNode)treeMap.get(leftChild[i]); //store it
possibleRoots.remove(new Integer(leftChild[i])); //remove from possible roots
}
if (rightChild[i] != -1) //if right child is present
{
((TreeNode)treeMap.get(i)).right = (TreeNode)treeMap.get(rightChild[i]); //store it
possibleRoots.remove(new Integer(rightChild[i])); //remove from possible roots
}
}
Iterator it = possibleRoots.iterator();
if (it.hasNext()) //if there is a root
inOrder((TreeNode)treeMap.get((Integer)it.next())); //run recursion on it
else result = false; //if no root, result is false
if (it.hasNext()) //if there is a second root
result = false; //result is false
for (i = 0; i < n; ++i) //for each node
if (!visited[i]) //if any node is unvisited
result = false; //result is false
return result;
}
public void inOrder(TreeNode root)
{
if (root == null || result == false) return; //if root is null or tree is already found to be invalid, return
if (visited[root.data]) //if node is already visited
{
result = false; //mark invalid
return; //return
}
visited[root.data] = true; //mark node as visited
inOrder(root.left); //recur on left child
inOrder(root.right); //recur on right child
}
}
/*Disjoint Set Solution*/
class Solution {
int[] parents;
public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
this.parents = new int[n];
int[] indegree = new int[n];
int connected = n;
for(int i=0; i<n; i++) {
parents[i] = i;
}
for(int i=0; i<leftChild.length; i++) {
if(leftChild[i] != -1) {
if(this.union(i, leftChild[i])) {
indegree[leftChild[i]]++;
connected--;
} else {
return false;
}
if(indegree[leftChild[i]] > 1) {
return false;
}
}
if(rightChild[i] != -1) {
if(this.union(i, rightChild[i])) {
indegree[rightChild[i]]++;
connected--;
} else {
return false;
}
if(indegree[rightChild[i]] > 1) {
return false;
}
}
}
return connected == 1;
}
private boolean union(int x, int y) {
int setX = find(x);
int setY = find(y);
if(setX != setY) {
parents[setY] = setX;
return true;
}
return false;
}
private int find(int num) {
if(parents[num] != num) {
parents[num] = find(parents[num]);
}
return parents[num];
}
}