Runtime error in C# but same code in Java works.


  • 0
    M

    This c# code fails with a runtime error, but if you run it as java (replace bool with boolean and LowestCommonAncestor with a lowercase l) it completes successfully. Why?

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public class Find
        {
            public TreeNode found = null;
            public bool p = false;
            public bool q = false;
            public Find(TreeNode found,bool p,bool q)
            {
                this.found=found;
                this.p=p;
                this.q=q;
            }
        }
        
        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            return helper(root, p, q).found;
        }
        
        public Find helper(TreeNode root,TreeNode p,TreeNode q)
        {
            Find temp = new Find(null,false,false);
            if(root == null)
            {
                return temp;
            }
            
            temp.p=(root == p);
            temp.q=(root == q);
            
            Find left = helper(root.left, p, q);
            if(left.found != null)
            {
                //temp.found = left.found;
                return left;
            }
            Find right = helper(root.right,p, q);
            
            
            if(right.found != null)
            {
                //temp.found = right.found;
                return right;
            }
            
            temp.p = temp.p | left.p | right.p;
            temp.q = temp.q | left.q | right.q;
            if(temp.p&&temp.q)
            {
                temp.found = root;
            }
            
            return temp;
        }
    }
    

    here is successful java code

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
            public class Find
        {
            public TreeNode found = null;
            public boolean p = false;
            public boolean q = false;
            public Find(TreeNode found,boolean p,boolean q)
            {
                this.found=found;
                this.p=p;
                this.q=q;
            }
        }
        
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            return helper(root, p, q).found;
        }
        
        public Find helper(TreeNode root,TreeNode p,TreeNode q)
        {
            Find temp = new Find(null,false,false);
            if(root == null)
            {
                return temp;
            }
            
            temp.p=(root == p);
            temp.q=(root == q);
            
            Find left = helper(root.left, p, q);
            if(left.found != null)
            {
                //temp.found = left.found;
                return left;
            }
            Find right = helper(root.right,p, q);
            
            
            if(right.found != null)
            {
                //temp.found = right.found;
                return right;
            }
            
            temp.p = temp.p | left.p | right.p;
            temp.q = temp.q | left.q | right.q;
            if(temp.p&&temp.q)
            {
                temp.found = root;
            }
            
            return temp;
        }
    }
    ``` collapse

  • 0
    R

    My C# code is also failing with Runtime Error for the test case with 10000 node tree (degenerate).
    Just tried it locally and it's getting stackoverflow due to the high depth of recursion.
    Most of the other high voted solutions are iterative, so I'm assuming we are expected to solve it iteratively.
    Why your java code is passing, I have no idea..

    Here's my code

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        
        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
        {
            TreeNode result = null;   
            LowestCommonAncestor(root, p, q, ref result);
            
            return result;
        }
    
        public static int LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q, ref TreeNode result)
        {
            if (root == null || p == null || q == null)
                return 0;
            
            int self1 = root == p ? 1 : 0;
            int self2 = root == q ? 1 : 0;
            
            int onLeft = LowestCommonAncestor(root.left, p, q, ref result);
            if(onLeft == 2) 
                return 2;
                
            int onRight = LowestCommonAncestor(root.right, p, q, ref result);
            if(onRight == 2) 
                return 2;
            
            int sum = self1 + self2 + onLeft + onRight;
            if(sum == 2)
                result = root;
                
            return sum;
        }
    }
    

Log in to reply
 

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