Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d11cea

Browse files
update: 110
1 parent a78a17b commit 1d11cea

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6565
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [JavaScript](./src/maximum-depth-of-binary-tree/res.js) | Easy |
6666
| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [JavaScript](./src/binary-tree-level-order-traversal-ii/res.js) | Easy |
6767
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [JavaScript](./src/convert-sorted-array-to-binary-search-tree/res.js) | Easy |
68+
| 110 | [balanced-binary-tree](https://leetcode.com/problems/balanced-binary-tree/) | [TypeScript](./src/balanced-binary-tree/res.ts) | Easy |
6869
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./src/minimum-depth-of-binary-tree/res.js) | Easy |
6970
| 120 | [Triangle](https://leetcode.com/problems/triangle/) | [JavaScript](./src/triangle/res.js) | Medium |
7071
| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [JavaScript](./src/best-time-to-buy-and-sell-stock/res.js) | Easy |

‎src/balanced-binary-tree/res.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Definition for a binary tree node.
3+
*/
4+
class TreeNode {
5+
val: number
6+
left: TreeNode | null
7+
right: TreeNode | null
8+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
9+
this.val = (val===undefined ? 0 : val)
10+
this.left = (left===undefined ? null : left)
11+
this.right = (right===undefined ? null : right)
12+
}
13+
}
14+
15+
function getTreeLength(root: TreeNode | null): number {
16+
if (!root) {
17+
return 0;
18+
} else {
19+
return 1 + Math.max(getTreeLength(root.left), getTreeLength(root.right));
20+
}
21+
}
22+
23+
/**
24+
* 自上而下递归
25+
*
26+
* 时间复杂度:O(n^2),其中 n 是二叉树中的节点个数。
27+
* 最坏情况下,二叉树是满二叉树,需要遍历二叉树中的所有节点,时间复杂度是 O(n)。
28+
* 对于节点 p,如果它的高度是 d,则 height(p) 最多会被调用 d 次(即遍历到它的每一个祖先节点时)。对于平均的情况,一棵树的高度 h 满足 O(h)=O(logn),因为 d≤h,所以总时间复杂度为 O(nlogn)。对于最坏的情况,二叉树形成链式结构,高度为 O(n),此时总时间复杂度为 O(n^2)。
29+
*
30+
* 空间复杂度:O(n),其中 n 是二叉树中的节点个数。空间复杂度主要取决于递归调用的层数,递归调用的层数不会超过 n。
31+
* @param root
32+
*/
33+
function isBalanced(root: TreeNode | null): boolean {
34+
if (!root || !root.left && !root.right) {
35+
return true;
36+
} else {
37+
return isBalanced(root.left) && isBalanced(root.right) && Math.abs(getTreeLength(root.left) - getTreeLength(root.right)) <= 1;
38+
}
39+
};
40+
41+
function getBalancedLength(root: TreeNode | null): number {
42+
if (!root) {
43+
return 0;
44+
} else {
45+
const leftLength = getBalancedLength(root.left);
46+
const rightLength = getBalancedLength(root.right);
47+
48+
if (leftLength < 0 || rightLength < 0 || Math.abs(leftLength - rightLength) > 1) {
49+
return -1;
50+
} else {
51+
return Math.max(leftLength, rightLength) + 1;
52+
}
53+
}
54+
}
55+
56+
/**
57+
* 自底向上的递归
58+
*
59+
* 复杂度均为 O(n)
60+
* @param root
61+
*/
62+
function isBalanced2(root: TreeNode | null): boolean {
63+
if (!root || !root.left && !root.right) {
64+
return true;
65+
} else {
66+
return getBalancedLength(root) >= 0;
67+
}
68+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /