-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
158 lines (137 loc) · 4.06 KB
/
Copy pathBalancedBinaryTree.java
File metadata and controls
158 lines (137 loc) · 4.06 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* <p>给定一个二叉树,判断它是否是高度平衡的二叉树。</p>
*
* <p>本题中,一棵高度平衡二叉树定义为:</p>
*
* <blockquote>
* <p>一个二叉树<em>每个节点 </em>的左右两个子树的高度差的绝对值不超过 1 。</p>
* </blockquote>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" />
* <pre>
* <strong>输入:</strong>root = [3,9,20,null,null,15,7]
* <strong>输出:</strong>true
* </pre>
*
* <p><strong>示例 2:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" />
* <pre>
* <strong>输入:</strong>root = [1,2,2,3,3,null,null,4,4]
* <strong>输出:</strong>false
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>root = []
* <strong>输出:</strong>true
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li>树中的节点数在范围 <code>[0, 5000]</code> 内</li>
* <li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
* </ul>
* <div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 855</li><li>👎 0</li></div>
*/
package leetcode2;
import java.util.HashMap;
import java.util.Map;
public class BalancedBinaryTree {
public static void main(String[] args) {
Solution solution = new BalancedBinaryTree().new Solution();
}
public 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 res = true;
public boolean isBalanced(TreeNode root) {
height(root);
return res;
}
private int height(TreeNode node) {
if (node == null) {
return 0;
}
int left = height(node.left);
int right = height(node.right);
if (Math.abs(left - right) > 1) {
res = false;
}
return Math.max(left, right) + 1;
}
}
class Solution1 {
public boolean isBalanced(TreeNode root) {
int val = height(root);
return val >= 0;
}
private int height(TreeNode node) {
if (node == null) {
return 0;
}
int left = height(node.left);
int right = height(node.right);
if (left < 0 || right < 0) return -1;
if (Math.abs(left - right) > 1) {
return -1;
}
return Math.max(left, right) + 1;
}
}
/**
* 自顶向下
*/
class Solution2 {
public boolean isBalanced(TreeNode root) {
Map<TreeNode, Integer> map = new HashMap<>();
return solve(root, map);
}
private boolean solve(TreeNode node, Map<TreeNode, Integer> map) {
if (node == null) {
return true;
}
int left = depth(map, node.left);
int right = depth(map, node.right);
return Math.abs(left - right) <= 1
&& solve(node.left, map)
&& solve(node.right, map);
}
private int depth(Map<TreeNode, Integer> map, TreeNode node) {
if (node == null) {
return 0;
}
Integer value = map.get(node);
if (value != null) {
return value;
}
int left = depth(map, node.left);
int right = depth(map, node.right);
value = Math.max(left, right) + 1;
map.put(node, value);
return value;
}
}
}