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);
    }
}

 


 

转载于:https://www.cnblogs.com/codingEskimo/p/6955084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值