mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(0110.平衡二叉树.md):增加typescript版本
This commit is contained in:
@ -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) {
|
||||||
|
Reference in New Issue
Block a user