Getting run time error on the C# code. Can anyone point whats wrong with the code?


  • 0
    H
    public class Solution {
        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
            {
                if (root == null || root == p || root == q) return root;
                TreeNode left = LowestCommonAncestor(root.left, p, q);
                TreeNode right = LowestCommonAncestor(root.right, p, q);
            
                if (left != null && right != null) 
                {return root;}
            
                if (left == null && right == null) 
                {return null;}
            
                return left != null ? left : right;
            }
    }
    

Log in to reply
 

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