We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 24e57b8 + ea5a011 commit de3fa62Copy full SHA for de3fa62
problems/0110.平衡二叉树.md
@@ -627,7 +627,26 @@ var isBalanced = function(root) {
627
};
628
```
629
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
648
## C
649
650
递归法:
651
```c
652
int getDepth(struct TreeNode* node) {
0 commit comments