Leetcode-549. Binary Tree Longest Consecutive Sequence II
来源:互联网 发布:windows 高精度sleep 编辑:程序博客网 时间:2024/04/26 13:50
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
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 valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.
Example 1:
Input: 1 / \ 2 3Output: 2Explanation: The longest consecutive path is [1, 2] or [2, 1].
Example 2:
Input: 2 / \ 1 3Output: 3Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].这个题目还是蛮简单的,有一个需要注意的地方就是必须是连续值。
public class Solution { private int max = 1; public int longestConsecutive(TreeNode root) { if(root == null) return 0; help(root); return max; } public int[] help(TreeNode root){ if(root == null) return null; int[] right = help(root.right); int[] left = help(root.left); int[] result = new int[]{1,1}; if(right == null && left == null) return result; if(root.right != null){ if(root.val == root.right.val + 1) { max = Math.max(max, right[0] + 1); result[0] = right[0] + 1; result[1] = 1; }else if(root.val == root.right.val - 1){ max = Math.max(max, right[1] + 1); result[0] = 1; result[1] = right[1] + 1; } } if(root.left != null){ if(root.val == root.left.val + 1) { max = Math.max(max, left[0] + result[1]); result[0] = Math.max(result[0], left[0] + 1); }else if(root.val == root.left.val - 1){ max = Math.max(max, left[1] + result[0]); result[1] = Math.max(result[1], left[1] + 1); } } return result; }}
0 0
- Leetcode-549. Binary Tree Longest Consecutive Sequence II
- [Leetcode] 549. Binary Tree Longest Consecutive Sequence II 解题报告
- 549. Binary Tree Longest Consecutive Sequence II
- LintCode Binary Tree Longest Consecutive Sequence II
- leetcode 298: Binary Tree Longest Consecutive Sequence
- Leetcode: 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
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
- 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
- The method getContextPath() from the type HttpServletRequest refers to the missing type
- Hexo NexT 主题下 blockquote 文本内容超出不自动换行解决方式
- 背包九讲
- 2017 蓝桥杯JavaB组省赛 日期问题
- LeetCode 111. Minimum Depth of Binary Tree
- Leetcode-549. Binary Tree Longest Consecutive Sequence II
- Vue2.0进阶组件篇4 突如其来(时间倒计时组件)
- 静态二维数组和动态二维数组(C语言)
- 170408 汇编-段内转移指令
- Unity3D游戏记录回放插件EZReplayManager(三)
- Windows 10中的PowerShell内置运行软件版本
- 谈谈我对云计算的认识之基本概念(二)
- LeetCode 226. Invert Binary Tree
- IntelliJ IDEA 14 注册码