-
Notifications
You must be signed in to change notification settings - Fork 43
/
BalancedBinaryTree.java
114 lines (96 loc) · 3.14 KB
/
BalancedBinaryTree.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
package LeetCodeJava.Recursion;
// https://leetcode.com/problems/balanced-binary-tree/
// A height-balanced binary tree
// is a binary tree in which the depth of the two subtrees of every node
// never differs by more than one.
import LeetCodeJava.DataStructure.TreeNode;
import java.util.Arrays;
import java.util.List;
public class BalancedBinaryTree {
// V0
// IDEA : POST TRAVERSE
// https://www.bilibili.com/video/BV1Ug411S7my/?share_source=copy_web
// V0'
// public boolean isBalanced(TreeNode root) {
//
// List<Integer> cache = Arrays.asList();
//
// return true;
// }
//
// private int getDepth(TreeNode node){
//
// if (node == null){
// return 0;
// }
//
// if (node.left != null){
// return getDepth(node.left) + 1;
// }
//
// if (node.right != null){
// return getDepth(node.right) + 1;
// }
//
// return 0;
// }
// V1
// IDEA : TOP DOWN RECURSION
// https://leetcode.com/problems/balanced-binary-tree/editorial/
// Recursively obtain the height of a tree. An empty tree has -1 height
private int height(TreeNode root) {
// An empty tree has height -1
if (root == null) {
return -1;
}
return 1 + Math.max(height(root.left), height(root.right));
}
public boolean isBalanced(TreeNode root) {
// An empty tree satisfies the definition of a balanced tree
if (root == null) {
return true;
}
// Check if subtrees have height within 1. If they do, check if the
// subtrees are balanced
return Math.abs(height(root.left) - height(root.right)) < 2
&& isBalanced(root.left)
&& isBalanced(root.right);
}
// V2
// IDEA : BOTTOM UP RECURSION
// https://leetcode.com/problems/balanced-binary-tree/editorial/
// define custom data structure
final class TreeInfo {
public final int height;
public final boolean balanced;
public TreeInfo(int height, boolean balanced) {
this.height = height;
this.balanced = balanced;
}
}
// the tree's height in a reference variable.
private TreeInfo isBalancedTreeHelper(TreeNode root) {
// An empty tree is balanced and has height = -1
if (root == null) {
return new TreeInfo(-1, true);
}
// Check subtrees to see if they are balanced.
TreeInfo left = isBalancedTreeHelper(root.left);
if (!left.balanced) {
return new TreeInfo(-1, false);
}
TreeInfo right = isBalancedTreeHelper(root.right);
if (!right.balanced) {
return new TreeInfo(-1, false);
}
// Use the height obtained from the recursive calls to
// determine if the current node is also balanced.
if (Math.abs(left.height - right.height) < 2) {
return new TreeInfo(Math.max(left.height, right.height) + 1, true);
}
return new TreeInfo(-1, false);
}
public boolean isBalanced_2(TreeNode root) {
return isBalancedTreeHelper(root).balanced;
}
}