Skip to content

Commit 97a1ef5

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题55 - II. 平衡二叉树
1 parent ecbdb35 commit 97a1ef5

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [面试题55 - II. 平衡二叉树](https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/)
2+
3+
## 题目描述
4+
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过 1,那么它就是一棵平衡二叉树。
5+
6+
**示例 1:**
7+
8+
给定二叉树 `[3,9,20,null,null,15,7]`
9+
10+
```
11+
3
12+
/ \
13+
9 20
14+
/ \
15+
15 7
16+
```
17+
18+
返回 `true`
19+
20+
**示例 2:**
21+
22+
给定二叉树 `[1,2,2,3,3,null,null,4,4]`
23+
24+
```
25+
1
26+
/ \
27+
2 2
28+
/ \
29+
3 3
30+
/ \
31+
4 4
32+
```
33+
34+
返回 `false`
35+
36+
**限制:**
37+
38+
- `1 <= 树的结点个数 <= 10000`
39+
40+
## 解法
41+
### Python3
42+
```python
43+
# Definition for a binary tree node.
44+
# class TreeNode:
45+
# def __init__(self, x):
46+
# self.val = x
47+
# self.left = None
48+
# self.right = None
49+
50+
class Solution:
51+
def isBalanced(self, root: TreeNode) -> bool:
52+
if root is None:
53+
return True
54+
return abs(self._height(root.left) - self._height(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
55+
56+
def _height(self, tree):
57+
if tree is None:
58+
return 0
59+
return 1 + max(self._height(tree.left), self._height(tree.right))
60+
```
61+
62+
### Java
63+
```java
64+
/**
65+
* Definition for a binary tree node.
66+
* public class TreeNode {
67+
* int val;
68+
* TreeNode left;
69+
* TreeNode right;
70+
* TreeNode(int x) { val = x; }
71+
* }
72+
*/
73+
class Solution {
74+
public boolean isBalanced(TreeNode root) {
75+
if (root == null) {
76+
return true;
77+
}
78+
return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
79+
}
80+
81+
private int height(TreeNode tree) {
82+
if (tree == null) {
83+
return 0;
84+
}
85+
return 1 + Math.max(height(tree.left), height(tree.right));
86+
}
87+
}
88+
```
89+
90+
### ...
91+
```
92+
93+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isBalanced(TreeNode root) {
12+
if (root == null) {
13+
return true;
14+
}
15+
return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
16+
}
17+
18+
private int height(TreeNode tree) {
19+
if (tree == null) {
20+
return 0;
21+
}
22+
return 1 + Math.max(height(tree.left), height(tree.right));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def isBalanced(self, root: TreeNode) -> bool:
10+
if root is None:
11+
return True
12+
return abs(self._height(root.left) - self._height(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
13+
14+
def _height(self, tree):
15+
if tree is None:
16+
return 0
17+
return 1 + max(self._height(tree.left), self._height(tree.right))

0 commit comments

Comments
 (0)