-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathques6.java
61 lines (49 loc) · 1.38 KB
/
ques6.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
//BALANCED BINARY TREE
// LEETCODE LINK --> https://leetcode.com/problems/balanced-binary-tree/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
boolean flag = true;
public int num(TreeNode root) {
if (root == null || !flag) {
return 0;
}
int a = num(root.right);
int b = num(root.left);
int k = Math.abs(a - b);
if (k > 1) {
flag = false;
}
return Math.max(a + 1, b + 1);
}
public boolean isBalanced(TreeNode root) {
num(root);
return flag;
}
}
public class ques6 {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(6);
root.left.left.left = new TreeNode(7);
Solution solution = new Solution();
boolean result = solution.isBalanced(root);
System.out.println(result);
}
}