Merge branch 'master' into master

This commit is contained in:
程序员Carl
2022-02-18 10:07:32 +08:00
committed by GitHub
33 changed files with 1315 additions and 226 deletions

View File

@@ -670,7 +670,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) {