Advertisement
1988coder

236. Lowest Common Ancestor of a Binary Tree

Oct 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 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 both nodes are present in the tree.
  13. Time Complexity : O(n) - Worst case all nodes will be visisted once.
  14. Space Complexity: O(n) - In worst case of skewed tree, the recursion stack can grow upto n.
  15. */
  16. class Solution {
  17.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  18.         if (root == null || root == p || root == q) {
  19.             return root;
  20.         }
  21.         TreeNode left = lowestCommonAncestor(root.left, p, q);
  22.         TreeNode right = lowestCommonAncestor(root.right, p, q);
  23.         if (left == null) {
  24.             return right;
  25.         } else if (right == null) {
  26.             return left;
  27.         } else {
  28.             return root;
  29.         }
  30.     }
  31. }
  32.  
  33. /**
  34. Recursive Solution
  35. This solution handles the input nodes if they are not present in the given tree. It will return null if it is not able to find either or both nodes in the tree.
  36. Time Complexity: O(n) - Worst case all nodes will be visited constant number of times.
  37. Space Complexity: O(n) - In worst case of skewed tree, the recursion stack can grow upto n.
  38. */
  39. class Solution {
  40.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  41.         if (root == null || root == p || root == q) {
  42.             return root;
  43.         }
  44.         boolean[] present = new boolean[2];
  45.         TreeNode lca = lowestCommonAncestorHelper(present, root, p, q);
  46.         if (present[0] && present[1]) {
  47.             return lca;
  48.         }
  49.         return null;
  50.     }
  51.    
  52.     public TreeNode lowestCommonAncestorHelper(boolean[] present, TreeNode node, TreeNode p, TreeNode q) {
  53.         if (node == null) {
  54.             return null;
  55.         }
  56.         TreeNode temp = null;
  57.         if (node == p) {
  58.             present[0] = true;
  59.             temp = node;
  60.         }
  61.         if (node == q) {
  62.             present[1] = true;
  63.             temp = node;
  64.         }
  65.         TreeNode left = lowestCommonAncestorHelper(present, node.left, p, q);
  66.         TreeNode right = lowestCommonAncestorHelper(present, node.right, p, q);
  67.         if (temp != null) {
  68.             return temp;
  69.         }
  70.         if (left == null) {
  71.             return right;
  72.         } else if (right == null) {
  73.             return left;
  74.         } else {
  75.             return node;
  76.         }
  77.     }
  78. }
  79.  
  80. /**
  81. Iterative Solution.
  82. BFS to find all node <-> parent relations. Then find the ancestors of P and then check if Q's ancestor part of P's ancestors.
  83. This solution assumes that both nodes are present in the tree.
  84. Time Complexity : O(n) - All nodes will be visisted constant number of times..
  85. Space Complexity: O(n) - all nodes will be saved in the HashMap
  86. */
  87. class Solution {
  88.     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  89.         if (root == null || root == p || root == q) {
  90.             return root;
  91.         }
  92.         HashMap<TreeNode, TreeNode> map = new HashMap<>();
  93.         Queue<TreeNode> queue = new LinkedList<>();
  94.         HashSet<TreeNode> ancestorsOfP = new HashSet<>();
  95.         TreeNode P = p;
  96.         TreeNode Q = q;
  97.         queue.offer(root);
  98.         map.put(root, null);
  99.         while (!queue.isEmpty() && (!map.containsKey(p) || !map.containsKey(q))) {
  100.             TreeNode node = queue.poll();
  101.             if (node.left != null) {
  102.                 map.put(node.left, node);
  103.                 queue.offer(node.left);
  104.             }
  105.             if (node.right != null) {
  106.                 map.put(node.right, node);
  107.                 queue.offer(node.right);
  108.             }
  109.         }
  110.         while (P != null) {
  111.             ancestorsOfP.add(P);
  112.             P = map.get(P);
  113.         }
  114.         while (!ancestorsOfP.contains(Q)) {
  115.             Q = map.get(Q);
  116.         }
  117.         return Q;
  118.     }
  119. }
  120.  
  121. // Also refer to https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/discuss/65245/Iterative-Solutions-in-PythonC++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement