My clean Java Solution without using global variable


  • 0
    M

    public class Solution {
    public int longestConsecutive(TreeNode root) {
    if (root == null) {
    return 0;
    }
    int[] max = new int[1];
    helper(root, 0, 0, max);
    return max[0];
    }
    public void helper(TreeNode root, int val, int len, int[] max){
    max[0] = Math.max(max[0], len);
    if(root == null) {
    return;
    }
    if(root.val == val + 1) {
    len++;
    } else {
    len=1;
    }
    helper(root.left, root.val, len, max);
    helper(root.right, root.val, len, max);
    }
    }~~

    • strikethrough text~~

Log in to reply
 

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