-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalancedBinaryTree.ts
87 lines (78 loc) · 2.2 KB
/
BalancedBinaryTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Source : https://leetcode.com/problems/balanced-binary-tree/
// Author : squxq
// Date : 2023-11-13
/*****************************************************************************************************
*
* Given a binary tree, determine if it is height-balanced.
*
* Example 1:
*
* Input: root = [3,9,20,null,null,15,7]
* Output: true
*
* Example 2:
*
* Input: root = [1,2,2,3,3,null,null,4,4]
* Output: false
*
* Example 3:
*
* Input: root = []
* Output: true
*
* Constraints:
*
* The number of nodes in the tree is in the range [0, 5000].
* -10^4 <= Node.val <= 10^4
******************************************************************************************************/
export type BT = NodeTree | null;
export class NodeTree {
val: number;
left: BT;
right: BT;
constructor(val?: number, left?: BT, right?: BT) {
this.val = val ?? 0;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}
/**
* BT -> boolean
* given a binry tree, determine if it is height-balanced
* NOTE: A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
* Stub:
function isBalanced(root: BT): boolean {return false}
* Tests:
* I: root = [3,9,20,null,null,15,7] -> O: true
* I: root = [1,2,2,3,3,null,null,4,4] -> O: false
* I: root = [] -> O: true
* Template:
function isBalanced(root: BT): boolean {
if (root === null) {return (...)}
else {
return (... (root.val)
(isBalanced(root.left))
(isBalanced(root.right)))
}
}
* CONSTRAINTS:
* - The number of nodes in the tree is in the range [0, 5000].
* - -10^4 <= Node.val <= 10^4
*/
export function isBalanced(root: BT): boolean {
if (root === null) {
return true;
}
const diff: number = Math.abs(height(root.left) - height(root.right));
return (
diff >= 0 && diff <= 1 && isBalanced(root.left) && isBalanced(root.right)
);
}
function height(node: BT): number {
if (node === null) {
return -1;
}
const leftHeight: number = height(node.left);
const rightHeight: number = height(node.right);
return Math.max(leftHeight, rightHeight) + 1;
}