Accepted C# Solution


  • 0
    A
    public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null || (root.left == null && root.right == null && root.val != p.val && root.val != q.val)) return null;
                if ((root.val <= p.val && root.val >= q.val) || (root.val >= p.val && root.val <= q.val))
                {
                    return root;
                }
                var left = LowestCommonAncestor(root.left, p, q);
                if (left != null) return left;
                var right = LowestCommonAncestor(root.right, p, q);
                return right;
     
        }
    

Log in to reply
 

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