Consecutive Tree Sequence
来源:互联网 发布:一个直播软件多少钱 编辑:程序博客网 时间:2024/06/05 04:26
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 parent to child (cannot be the reverse
).
1 \ 3 / \ 2 4 \ 5
2 \ 3 / 2 / 1
/** * 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 */ /* int max = 0; public int longestConsecutive(TreeNode root) { // Write your code here if (root == null) return 0; helper(root, 0, root.val); //0代表curLen, 开始递归时,root 还未处理 return max; } private void helper(TreeNode root, int curLen, int target){ //preorder traversal if (root == null){ return; } if (root.val == target){ curLen++; } else { curLen = 1; } max = Math.max(max, curLen); helper(root.left, curLen, root.val + 1); helper(root.right, curLen, root.val + 1); } */ //postOrder traversal /* private int longest = 0; public int longestConsecutive(TreeNode root) { helper(root); return longest; } private int helper(TreeNode root){ //base case if (root == null){ return 0; } int curLen = 1; //at least there is root //divide int left = helper(root.left); int right = helper(root.right); //conquer if (root.left != null && root.val + 1 == root.left.val){ curLen = left + 1; } if (root.right != null && root.val + 1 == root.right.val){ curLen = Math.max(curLen, right + 1); //need to get the longer path from right and left } //otherwise longest will 1 to pass on to the upper level if (curLen > longest){ longest = curLen; } return curLen; } */ private class ResultType{ int curLen; // the curLen in this node as ending node, traverse from bottom to top int maxLen; // in the subtree and itself public ResultType(int curLen, int maxLen){ this.curLen = curLen; this.maxLen = maxLen; } } //how to avoid global variables, using postOrder traversal public int longestConsecutive(TreeNode root) { ResultType res = helper(root); return res.maxLen; } private ResultType helper(TreeNode root){ if (root == null){ return new ResultType(0, 0); } ResultType left = helper(root.left); ResultType right = helper(root.right); //how to update the curLen and maxLen int curLen = 1; if (root.left != null && root.val + 1 == root.left.val){ curLen = 1 + left.curLen; } if (root.right != null && root.val + 1 == root.right.val){ curLen = Math.max(curLen, 1 + right.curLen); } int maxLen = Math.max(curLen, Math.max(left.maxLen, right.maxLen)); return new ResultType(curLen, maxLen); }}
0 0
- Consecutive Tree Sequence
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence
- leetcode 298: Binary Tree Longest Consecutive Sequence
- Leetcode: Binary Tree Longest Consecutive Sequence
- [LeetCode298]Binary Tree Longest Consecutive Sequence
- 298. Binary Tree Longest Consecutive Sequence
- Leetcode 298Binary Tree Longest Consecutive Sequence
- [Leetcode]Binary Tree Longest Consecutive Sequence
- LeetCode 298. Binary Tree Longest Consecutive Sequence
- Leetcode Binary Tree Longest Consecutive Sequence
- LeetCode 298. Binary Tree Longest Consecutive Sequence
- LintCode Binary Tree Longest Consecutive Sequence II
- 298. Binary Tree Longest Consecutive Sequence
- ThinkPHP框架----控制器调用模型里的方法,一直报错——Think\Model:..方法不存在!
- 【Oracle批量更新】根据一个大表批量更新另一大表的方法比较
- [Node.js]EventEmitter
- ie8不支持fixed以及jquery版本问题
- Oracle中多表联合更新处理方法详解
- Consecutive Tree Sequence
- mac 安装消息中间件---ActiveMQ
- Dlib+Vs2015配置
- winform 把窗口上的全部信息生成PDF文件显示
- 三星电子及时处理Note7燃损原因已查明
- C++编码器军规(003):避免对一个声明为const的变量使用const_cast
- 2016年知名企业阵亡名单丨不在变革中爆发,就在骄傲中死亡
- QString 保留小数点后一位数 & QT int 转16进制QString并补零
- 1.跨域资源共享资源CORS