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);
    }
}
时间: 2024-12-28 08:50:48

Binary Tree Longest Consecutive Sequence II的相关文章

LeetCode 549. Binary Tree Longest Consecutive Sequence II

Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not v

[Locked] Binary Tree Longest Consecutive Sequence

Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The l

Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Binary Tree Longest Consecutive Sequence Leetcode

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Leetcode: Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

298. Binary Tree Longest Consecutive Sequence

题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be fr

[LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

[LC] 298. Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Binary Tree Longest Consecutive Sequence III

Note: This question is good summary for this kind of problem. 1) Once you get the root, loop through all the children. Get the max up/down/max from the children. 2) set root max = up + down + 1, then comparing with the max got from the children. 3) R