Advertisement
sweet1cris

Untitled

Sep 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. Dr. Rick Sun code    
  2.  public TreeNode LCA(TreeNode root, TreeNode a, TreeNode b) {
  3.         if (root == null ||root == a || root == b) return root;// base case 1; case 3
  4.         TreeNode left = LCA(root.left, a, b);
  5.         TreeNode right = LCA(root.right, a, b); // recursion step 1
  6.         if (left != null && right != null) return root;// recursion step 2 + 3; case 1
  7.         return (left != null ? left : right);                                // case 2
  8.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement