-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0333-largest-bst-subtree.js
More file actions
52 lines (48 loc) · 1.57 KB
/
0333-largest-bst-subtree.js
File metadata and controls
52 lines (48 loc) · 1.57 KB
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
/**
* 333. Largest BST Subtree
* https://leetcode.com/problems/largest-bst-subtree/
* Difficulty: Medium
*
* Given the root of a binary tree, find the largest subtree, which is also a Binary Search
* Tree (BST), where the largest means subtree has the largest number of nodes.
*
* A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
* properties:
* - The left subtree values are less than the value of their parent (root) node's value.
* - The right subtree values are greater than the value of their parent (root) node's value.
*
* Note: A subtree must include all of its descendants.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var largestBSTSubtree = function(root) {
let maxSize = 0;
traverse(root);
return maxSize;
function traverse(node) {
if (!node) return { isBST: true, size: 0, min: Infinity, max: -Infinity };
const left = traverse(node.left);
const right = traverse(node.right);
if (left.isBST && right.isBST && node.val > left.max && node.val < right.min) {
const size = left.size + right.size + 1;
maxSize = Math.max(maxSize, size);
return {
isBST: true,
size,
min: Math.min(left.min, node.val),
max: Math.max(right.max, node.val)
};
}
return { isBST: false, size: 0, min: 0, max: 0 };
}
};