[LeetCode]Binary Tree Longest Consecutive Sequence

public class Solution {
    private int result = 0;
    public int longestConsecutive(TreeNode root) {
        if (root == null) {
            return 0;
        }
        helper(root, root.val, 0);
        return result;
    }
    public void helper(TreeNode root, int pre, int count) {
        if (root == null) {
            return;
        }
        if (root.val == pre + 1) {
            count ++;
        } else {
            count = 1;
        }
        result = Math.max(result, count);
        if (root.left != null) {
            helper(root.left, root.val, count);
        }
        if (root.right != null) {
            helper(root.right, root.val, count);
        }
    }
}

 

posted @ 2015-12-01 09:54  Weizheng_Love_Coding  阅读(134)  评论(0编辑  收藏  举报