We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d5c95dc commit e3caee6Copy full SHA for e3caee6
Problems/03-Balanced-Binary-Tree/Balanced-Binary-Tree.ts
@@ -0,0 +1,20 @@
1
+function isBalanced(root: TreeNode | null): boolean {
2
+ if (!root) {
3
+ return true;
4
+ }
5
+ console.log(balance(root));
6
+ return balance(root) !== -1
7
+
8
+ //自底至顶 后续遍历
9
+ function balance(root: TreeNode | null): number {
10
11
+ return 0;
12
13
+ let leftDepth = balance(root.left);
14
+ if (leftDepth == -1) return -1;
15
+ let rightDepth = balance(root.right);
16
+ if (rightDepth == -1) return -1;
17
+ if (Math.abs(leftDepth - rightDepth) > 1) return -1;
18
+ return 1 + Math.max(leftDepth, rightDepth);
19
20
+};
0 commit comments