This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Proposed solution to find the largest Binary Search Tree in a Binary Tree. | |
* | |
* Proposed Solution: | |
* To find the largest BST in a tree there are different options to traverse the tree: we could take an top-down approach | |
* or a bottom-up. | |
* | |
* The top-down approach require us to check at each node from the root if the tree starting at that node is a BST. This | |
* makes us traverse multiple times the tree to find the bst. Although it stops as soon as it find a BST because it will be | |
* the largest. This solution has a time complexity of O(n^2) in worst-case (degenerated tree into list) or O(nLogn) (balanced tree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LargestBst{ | |
public static class TreeNode { | |
int value; | |
TreeNode left; | |
TreeNode right; | |
} | |
public static class TreeNodeHelper { | |
TreeNode node; |