Same logic, using c# is fail , using java will pass


  • 0
    T

    C# 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 int LongestConsecutive(TreeNode root) {
            return Helper(root)[2];
        }
        
        // 0 -> desc
        // 1 -> asc
        // 2 -> maxresult
        private int[] Helper(TreeNode root){
            int[] result = new int[3];
            if(root == null)
                return result;
            
            result[0] = 1;
            result[1] = 1;
            result[2] = 1;
            
            int[] leftR = Helper(root.left);
            int[] rightR = Helper(root.right);
            
            int r1 = 1;
            if(leftR[0] != 0 && root.val == (long)root.left.val+1 ){
                r1+=leftR[0];
                result[0] = Math.Max(result[0],leftR[0]+1);
            }
            if(rightR[1] != 0 && root.val == (long)root.right.val-1 ){
                r1+=rightR[1];
                result[1] = Math.Max(result[1],rightR[1]+1);
            }
            int r2 = 1;
            if(rightR[0] != 0 && root.val == (long)root.right.val+1 ){
                r2+=rightR[0];
                result[0] = Math.Max(result[0],rightR[0]+1);
            }
            if(leftR[1] != 0 && root.val == (long)root.left.val-1 ){
                r2+=leftR[1];
                result[1] = Math.Max(result[1],leftR[1]+1);
            }
            
            result[2] = Math.Max(Math.Max(r1,r2),1);
            result[2] = Math.Max(result[2],Math.Max(leftR[2],rightR[2]));
            
            return result;
        }
    }
    

    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 int longestConsecutive(TreeNode root) {
            return Helper(root)[2];
        }
        
        // 0 -> desc
        // 1 -> asc
        // 2 -> maxresult
        private int[] Helper(TreeNode root){
            int[] result = new int[3];
            if(root == null)
                return result;
            
            result[0] = 1;
            result[1] = 1;
            result[2] = 1;
            
            int[] leftR = Helper(root.left);
            int[] rightR = Helper(root.right);
            
            int r1 = 1;
            if(leftR[0] != 0 && root.val == (long)root.left.val+1 ){
                r1+=leftR[0];
                result[0] = Math.max(result[0],leftR[0]+1);
            }
            if(rightR[1] != 0 && root.val == (long)root.right.val-1 ){
                r1+=rightR[1];
                result[1] = Math.max(result[1],rightR[1]+1);
            }
            int r2 = 1;
            if(rightR[0] != 0 && root.val == (long)root.right.val+1 ){
                r2+=rightR[0];
                result[0] = Math.max(result[0],rightR[0]+1);
            }
            if(leftR[1] != 0 && root.val == (long)root.left.val-1 ){
                r2+=leftR[1];
                result[1] = Math.max(result[1],leftR[1]+1);
            }
            
            result[2] = Math.max(Math.max(r1,r2),1);
            result[2] = Math.max(result[2],Math.max(leftR[2],rightR[2]));
            
            return result;
        }
    }
    

Log in to reply
 

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