首页 > 代码库 > Binary Tree Longest Consecutive Sequence II

Binary Tree Longest Consecutive Sequence II

Note: O(n)

This question is the extension of the version one. The longest consecutive Sequence could be found in "Parent->Children" or "Children -> Parent -> Children". The first situation is the same as previous one. The second is more complicated. We need to consider the directions, increasing and decreasing. Once we know at each node the longest increasing and decreasing, the sum should be its longest consecutive sequence.
For example:
1) both children are not consecutive. up = 0, down = 0, max = 0;

2) one children is not consecutive. This is equal to Parent->Children. up/down = x, the ohter= 0, max = x;

3) they all consecutive. "children -> parent -> children", up/down = x, the other = y, max = x + y;

In the code, each time to set up "up = 0, down = 0" so if it is not consequence, the left/right.up/down is not accessible. So we can make sure only once it is consecutive, we will comparing the maxUpLen and maxDownLen. Then we will keep the maxlen in len. If the consecutive is broken, and start another one, the maxUpLen, and maxDownLen is actually only keeps the most recent record. But len keeps the globle max len record. Because from the code we know, up and down has been reset each time, but len is never reset. 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * @param root the root of binary tree
     * @return the length of the longest consecutive sequence path
     */
    private class ResultType {
        int len;
        int maxUpLen;
        int maxDownLen;
        public ResultType(int len, int maxUpLen, int maxDownLen) {
            this.len = len;
            this.maxUpLen = maxUpLen;
            this.maxDownLen = maxDownLen;
        }
    }
    public int longestConsecutive2(TreeNode root) {
        // Write your code here
        ResultType rst = findLongestConsecutive(root);
        return rst.len;
    }
    
    private ResultType findLongestConsecutive(TreeNode root) {
        if (root == null) {
            return new ResultType(0, 0, 0);
        }
        
        ResultType left = findLongestConsecutive(root.left);
        ResultType right = findLongestConsecutive(root.right);
        
        //set up both up and down to zero is very important. 
        //if it is not consecutive, the result will be set to zero
        int up = 0;
        int down = 0;
        
        
        //This is get the max up and down consecutive considering both side
        if (root.left != null && root.left.val + 1 == root.val) {
            down = Math.max(down, left.maxDownLen + 1);
        }
        
        if (root.left != null && root.left.val - 1 == root.val) {
            up = Math.max(up, left.maxUpLen + 1);
        }
        
        if (root.right != null && root.right.val + 1 == root.val) {
            down = Math.max(down, right.maxDownLen + 1);
        }
        
        if (root.right != null && root.right.val - 1 == root.val) {
            up = Math.max(up, right.maxUpLen + 1);
        }
        
        int len = down + up + 1;
        len = Math.max(len, Math.max(left.len, right.len));
        return new ResultType(len, up, down);
    }
}

 


 

Binary Tree Longest Consecutive Sequence II