Merge pull request #1074 from xiaofei-2020/tree12

添加(0110.平衡二叉树.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-02-18 10:05:56 +08:00
committed by GitHub

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
递归法: 递归法:
```c ```c
int getDepth(struct TreeNode* node) { int getDepth(struct TreeNode* node) {