6 lines C# recursive solution


  • 0
    K
    public class Solution {
        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            
            if ((p.val < root.val && q.val > root.val) || p.val == root.val || q.val == root.val ||(q.val < root.val && p.val > root.val))
                return root;
            else if (p.val > root.val && q.val > root.val)
                return LowestCommonAncestor(root.right,p,q);
            else // (p.val < root.val && q.val < root.val)
                return LowestCommonAncestor(root.left,p,q);        
        }
    }
    

Log in to reply
 

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