Skip to content

Instantly share code, notes, and snippets.

View cvielma's full-sized avatar

Christian Vielma cvielma

View GitHub Profile
@cvielma
cvielma / LargestBSTFull.java
Last active December 23, 2015 13:39
Largest BST in a Tree - A full Java Solution
/**
* 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)
@cvielma
cvielma / LargestBst.java
Last active December 23, 2015 13:39
Largest BST in a Tree - Java Solution (short)
public class LargestBst{
public static class TreeNode {
int value;
TreeNode left;
TreeNode right;
}
public static class TreeNodeHelper {
TreeNode node;