deno.land / x / masx200_leetcode_test@10.6.5 / balanced-binary-tree / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
export default function isBalanced(root: TreeNode | null): boolean { return height(root) >= 0;}function height(root: TreeNode | null): number { if (!root) return 0; const leftHeight = height(root.left); const rightHeight = height(root.right); if ( leftHeight === -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1 ) { return -1; } else { return Math.max(leftHeight, rightHeight) + 1; }}
masx200_leetcode_test

Version Info

Tagged at
a year ago