Skip to content

Commit de3fa62

Browse files
Merge pull request youngyangyang04#1074 from xiaofei-2020/tree12
添加(0110.平衡二叉树.md):增加typescript版本
2 parents 24e57b8 + ea5a011 commit de3fa62

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

problems/0110.平衡二叉树.md

+19
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,26 @@ var isBalanced = function(root) {
627627
};
628628
```
629629

630+
## TypeScript
631+
632+
```typescript
633+
// 递归法
634+
function isBalanced(root: TreeNode | null): boolean {
635+
function getDepth(root: TreeNode | null): number {
636+
if (root === null) return 0;
637+
let leftDepth: number = getDepth(root.left);
638+
if (leftDepth === -1) return -1;
639+
let rightDepth: number = getDepth(root.right);
640+
if (rightDepth === -1) return -1;
641+
if (Math.abs(leftDepth - rightDepth) > 1) return -1;
642+
return 1 + Math.max(leftDepth, rightDepth);
643+
}
644+
return getDepth(root) !== -1;
645+
};
646+
```
647+
630648
## C
649+
631650
递归法:
632651
```c
633652
int getDepth(struct TreeNode* node) {

0 commit comments

Comments
 (0)