添加(0110.平衡二叉树.md):增加typescript版本

This commit is contained in:
Steve2020
2022-02-05 13:04:38 +08:00
parent 97bc7efbb2
commit ea5a011d78

View File

@ -627,7 +627,26 @@ var isBalanced = function(root) {
};
```
## TypeScript
```typescript
// 递归法
function isBalanced(root: TreeNode | null): boolean {
function getDepth(root: TreeNode | null): number {
if (root === null) return 0;
let leftDepth: number = getDepth(root.left);
if (leftDepth === -1) return -1;
let rightDepth: number = getDepth(root.right);
if (rightDepth === -1) return -1;
if (Math.abs(leftDepth - rightDepth) > 1) return -1;
return 1 + Math.max(leftDepth, rightDepth);
}
return getDepth(root) !== -1;
};
```
## C
递归法:
```c
int getDepth(struct TreeNode* node) {