3 line java answer.


  • 0
    public class Solution {
        public TreeNode lca(TreeNode root, TreeNode p, TreeNode q) {   
            if(root == null || p == root || q == root) return root;
            TreeNode left = lca(root.left, p, q); TreeNode right = lca(root.right, p, q);
            if(left == null) return right; else if(right == null) return left; else return root;
        }
    }
    

  • 0
    K

    Shameful you call this three line


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.