LeetCode- Binary Tree Longest Consecutive Sequence
2016-08-21 14:51
453 查看
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 parent to child (cannot be the reverse).
For example,
Longest consecutive sequence path is
Longest consecutive sequence path is
Solution:
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 parent to child (cannot be the reverse).
For example,
1 \ 3 / \ 2 4 \ 5
Longest consecutive sequence path is
3-4-5, so return
3.
2 \ 3 / 2 / 1
Longest consecutive sequence path is
2-3,not
3-2-1, so return
2.
Solution:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public class ConsLen { int fromMe; int fromBelow; public ConsLen(int m, int b){ fromMe = m; fromBelow = b; } } public int longestConsecutive(TreeNode root) { if (root==null) return 0; ConsLen res = findConsecutiveSeq(root); return Math.max(res.fromMe,res.fromBelow); } public ConsLen findConsecutiveSeq(TreeNode cur){ if (cur.left==null && cur.right==null){ return new ConsLen(1,0); } ConsLen res = new ConsLen(1,0); if (cur.left!=null){ ConsLen leftRes = findConsecutiveSeq(cur.left); if (cur.left.val == cur.val+1){ res.fromMe = leftRes.fromMe + 1; } res.fromBelow = Math.max(leftRes.fromMe, leftRes.fromBelow); } if (cur.right!=null){ ConsLen rightRes = findConsecutiveSeq(cur.right); if (cur.right.val == cur.val+1){ res.fromMe = Math.max(res.fromMe,rightRes.fromMe+1); } res.fromBelow = Math.max(res.fromBelow,Math.max(rightRes.fromMe,rightRes.fromBelow)); } return res; } }
相关文章推荐
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
- Leetcode 298 Binary Tree Longest Consecutive Sequence
- [LeetCode] Binary Tree Longest Consecutive Sequence
- Leetcode Binary Tree Longest Consecutive Sequence
- LeetCode "Binary Tree Longest Consecutive Sequence"
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
- Leetcode: Binary Tree Longest Consecutive Sequence
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
- [Leetcode]Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence -- LeetCode
- [LeetCode298]Binary Tree Longest Consecutive Sequence
- leetcode 298: Binary Tree Longest Consecutive Sequence
- [leetcode 298] Binary Tree Longest Consecutive Sequence---求二叉树连续序列的长度
- Leetcode: Binary Tree Longest Consecutive Sequence
- LeetCode Binary Tree Longest Consecutive Sequence
- [LeetCode]Binary Tree Longest Consecutive Sequence
- [Locked] Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III