Largest Binary Search Tree (BST) in a Binary Tree | LeetCode
Given a binary tree, find the largest Binary Search Tree (BST), where largest means BST with largest number of nodes in it. The largest BST may or may not include all of its descendants.
// The largest BST may or may not include all of its descendants.
public static int largestBSTSubtree(Node node) {
if (node == null)
return 0;
if (node.left == null && node.right == null)
return 1;
int leftNode = largestBSTSubtree(node.left);
int rightNode = largestBSTSubtree(node.right);
if (node.left != null && node.right != null) {
if ((node.left.value < node.value) && (node.right.value > node.value)) {
return leftNode + rightNode + 1;
} else if (node.left.value < node.value) {
return leftNode + 1;
} else if (node.right.value > node.value) {
return rightNode + 1;
} else {
return Math.max(rightNode, leftNode);
}
} else if (node.left != null) {
if (node.left.value < node.value)
return leftNode + 1;
else
return leftNode;
} else {// if (node.right != null){
if (node.value < node.right.value)
return rightNode + 1;
else
return rightNode;
}
}
// The largest BST may or may not include all of its descendants.
public static LargestBST largestBSTSubtree1(Node node) {
if (node == null)
return null;
if (node.left == null && node.right == null) {
return new LargestBST(node, node.size(), node.value, node.value);
}
LargestBST leftNode = largestBSTSubtree1(node.left);
LargestBST rightNode = largestBSTSubtree1(node.right);
if (leftNode != null && rightNode != null) {
if ((node.value > leftNode.max && node.left == leftNode.node)
&& (node.value < rightNode.min && node.right == rightNode.node)) {
LargestBST bst = new LargestBST(node,
leftNode.maxNode + rightNode.maxNode + 1,
leftNode.min,
rightNode.max);
return bst;
} else if (node.value > leftNode.max && node.left == leftNode.node) {
return new LargestBST(node, leftNode.maxNode + 1, leftNode.min, node.value);
} else if (node.value < rightNode.min && node.right == rightNode.node) {
return new LargestBST(node, rightNode.maxNode + 1, node.value, rightNode.max);
} else {
return (leftNode.maxNode > rightNode.maxNode) ? leftNode : rightNode;
}
} else if (leftNode != null) {
if (node.value > leftNode.max && node.left == leftNode.node) {
return new LargestBST(node, leftNode.maxNode + 1, leftNode.min, node.value);
} else {
return leftNode;
}
} else if (rightNode != null) {
if (node.value < rightNode.min && node.right == rightNode.node) {
return new LargestBST(node, rightNode.maxNode + 1, node.value, rightNode.max);
} else {
return rightNode;
}
}
return null;
}
http://www.careercup.com/question?id=20347665
http://www.quora.com/How-can-we-find-the-largest-binary-search-tree-in-a-binary-tree-efficiently
Read full article from Largest Binary Search Tree (BST) in a Binary Tree | LeetCode
Given a binary tree, find the largest Binary Search Tree (BST), where largest means BST with largest number of nodes in it. The largest BST may or may not include all of its descendants.
___________________15___________________ / \ ___________10___________ 20 / \ 5 _____7____ / \ __2__ __5 / \ / 0 8 3
The largest BST (may or may not include all of its descendants) from the above example should be:
____15____ / \ _10 20 / 5
// The min and max values are passed top-down to check if
// including a node satisfies the current BST constraint.
// The child nodes are passed bottom-up to be assigned
// to its parent.
// Returns the total number of nodes the child holds.
int findLargestBST(BinaryTree *p, int min, int max, int &maxNodes,
BinaryTree *& largestBST, BinaryTree *& child) {
if (!p) return 0;
if (min < p->data && p->data < max) {
int leftNodes = findLargestBST(p->left, min, p->data, maxNodes, largestBST, child);
BinaryTree *leftChild = (leftNodes == 0) ? NULL : child;
int rightNodes = findLargestBST(p->right, p->data, max, maxNodes, largestBST, child);
BinaryTree *rightChild = (rightNodes == 0) ? NULL : child;
// create a copy of the current node and
// assign its left and right child.
BinaryTree *parent = new BinaryTree(p->data);
parent->left = leftChild;
parent->right = rightChild;
// pass the parent as the child to the above tree.
child = parent;
int totalNodes = leftNodes + rightNodes + 1;
if (totalNodes > maxNodes) {
maxNodes = totalNodes;
largestBST = parent;
}
return totalNodes;
} else {
// include this node breaks the BST constraint,
// so treat this node as an entirely new tree and
// check if a larger BST exist in this tree
findLargestBST(p, INT_MIN, INT_MAX, maxNodes, largestBST, child);
// must return 0 to exclude this node
return 0;
}
}
BinaryTree* findLargestBST(BinaryTree *root) {
BinaryTree *largestBST = NULL;
BinaryTree *child;
int maxNodes = INT_MIN;
findLargestBST(root, INT_MIN, INT_MAX, maxNodes, largestBST, child);
return largestBST;
}
https://github.com/xiaoningning/algorithm/blob/master/PuzzleCoding/src/LargestBSTSubtree.java// The largest BST may or may not include all of its descendants.
public static int largestBSTSubtree(Node node) {
if (node == null)
return 0;
if (node.left == null && node.right == null)
return 1;
int leftNode = largestBSTSubtree(node.left);
int rightNode = largestBSTSubtree(node.right);
if (node.left != null && node.right != null) {
if ((node.left.value < node.value) && (node.right.value > node.value)) {
return leftNode + rightNode + 1;
} else if (node.left.value < node.value) {
return leftNode + 1;
} else if (node.right.value > node.value) {
return rightNode + 1;
} else {
return Math.max(rightNode, leftNode);
}
} else if (node.left != null) {
if (node.left.value < node.value)
return leftNode + 1;
else
return leftNode;
} else {// if (node.right != null){
if (node.value < node.right.value)
return rightNode + 1;
else
return rightNode;
}
}
// The largest BST may or may not include all of its descendants.
public static LargestBST largestBSTSubtree1(Node node) {
if (node == null)
return null;
if (node.left == null && node.right == null) {
return new LargestBST(node, node.size(), node.value, node.value);
}
LargestBST leftNode = largestBSTSubtree1(node.left);
LargestBST rightNode = largestBSTSubtree1(node.right);
if (leftNode != null && rightNode != null) {
if ((node.value > leftNode.max && node.left == leftNode.node)
&& (node.value < rightNode.min && node.right == rightNode.node)) {
LargestBST bst = new LargestBST(node,
leftNode.maxNode + rightNode.maxNode + 1,
leftNode.min,
rightNode.max);
return bst;
} else if (node.value > leftNode.max && node.left == leftNode.node) {
return new LargestBST(node, leftNode.maxNode + 1, leftNode.min, node.value);
} else if (node.value < rightNode.min && node.right == rightNode.node) {
return new LargestBST(node, rightNode.maxNode + 1, node.value, rightNode.max);
} else {
return (leftNode.maxNode > rightNode.maxNode) ? leftNode : rightNode;
}
} else if (leftNode != null) {
if (node.value > leftNode.max && node.left == leftNode.node) {
return new LargestBST(node, leftNode.maxNode + 1, leftNode.min, node.value);
} else {
return leftNode;
}
} else if (rightNode != null) {
if (node.value < rightNode.min && node.right == rightNode.node) {
return new LargestBST(node, rightNode.maxNode + 1, node.value, rightNode.max);
} else {
return rightNode;
}
}
return null;
}
http://www.careercup.com/question?id=20347665
http://www.quora.com/How-can-we-find-the-largest-binary-search-tree-in-a-binary-tree-efficiently
Read full article from Largest Binary Search Tree (BST) in a Binary Tree | LeetCode