Advertisement
1988coder

235. Lowest Common Ancestor of a Binary Search Tree

Oct 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode(int x) { val = x; }
  8.  * }
  9.  */
  10. /**
  11. Recursive Solution
  12. This solution assumes that the tree has unique numbers. And both p & q exists in the tree.
  13. Time Complexity: O(n) -> In worst case all nodes of the tree will be visited.
  14. Space Complexity: O(height of the tree) -> In worst case height can be n if the tree is skewed.
  15. */
  16. class Solution {
  17.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  18.         if (root == null) {
  19.             return null;
  20.         }
  21.         if (p.val < root.val && q.val < root.val) {
  22.             return lowestCommonAncestor(root.left, p, q);
  23.         } else if (p.val > root.val && q.val > root.val) {
  24.             return lowestCommonAncestor(root.right, p, q);
  25.         } else {
  26.             return root;
  27.         }
  28.     }
  29. }
  30.  
  31. /**
  32. Iterative Solution
  33. This solution assumes that the tree has unique numbers. And both p & q exists in the tree.
  34. Time Complexity: O(height of the BST) -> In worst case height can be all nodes if the tree is skewed.
  35. Space Complexity: O(1)
  36. */
  37. class Solution {
  38.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  39.         if (root == null) {
  40.             return null;
  41.         }
  42.         TreeNode cur = root;
  43.         while (cur != null) {
  44.             if (p.val < cur.val && q.val < cur.val) {
  45.                 cur = cur.left;
  46.             } else if (p.val > cur.val && q.val > cur.val) {
  47.                 cur = cur.right;
  48.             } else {
  49.                 return cur;
  50.             }
  51.         }
  52.         return null;
  53.     }
  54. }
  55.  
  56. /**
  57. Find the paths to p & q and then check for lca
  58. Time Complexity:
  59. findPath takes O(Tree Height) * 2
  60. Comparing ancestors is O(Tree Height) as the ancestors array size can be max Tree Height.
  61. Thus, total Time Complexity = O(Tree Height) -> tree height in case of skewed tree will be equal to number of nodes in the tree.
  62. Space Complexity: O(Tree Height)
  63. */
  64. class Solution {
  65.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  66.         if (root == null || p == null || q == null) {
  67.             return null;
  68.         }
  69.         List<TreeNode> ancestorsP = new ArrayList<>();
  70.         List<TreeNode> ancestorsQ = new ArrayList<>();
  71.        
  72.         if (!findPath(ancestorsP, root, p)) {
  73.             return null;
  74.         }
  75.         if (!findPath(ancestorsQ, root, q)) {
  76.             return null;
  77.         }
  78.         TreeNode lca = null;
  79.         for (int i = 0; i < ancestorsP.size() && i < ancestorsQ.size(); i++) {
  80.             if (ancestorsP.get(i) != ancestorsQ.get(i)) {
  81.                 break;
  82.             }
  83.             lca = ancestorsP.get(i);
  84.         }
  85.         return lca;
  86.     }
  87.    
  88.     public boolean findPath(List<TreeNode> ancestors, TreeNode root, TreeNode node) {
  89.         if (root == null) {
  90.             return false;
  91.         }
  92.         ancestors.add(root);
  93.         if (root == node) {
  94.             return true;
  95.         }
  96.         if (node.val < root.val) {
  97.             return findPath(ancestors, root.left, node);
  98.         } else {
  99.             return findPath(ancestors, root.right, node);
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement