LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/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).
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,not3-2-1, so return 2.
题解:
DFS bottom-up方法.
Time Complexity: O(n). Space: O(logn).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int longestConsecutive(TreeNode root) {
dfs(root);
return max;
} private int dfs(TreeNode root){
if(root == null){
return 0;
} int l = dfs(root.left)+1;
int r = dfs(root.right)+1;
if(root.left != null && root.val != root.left.val-1){
l = 1;
} if(root.right != null && root.val != root.right.val-1){
r = 1;
} int curMax = Math.max(l, r);
max = Math.max(max, curMax);
return curMax;
}
}
也可采用top down的方法.
Time Complexity: O(n). Space: O(logn).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestConsecutive(TreeNode root) {
if(root == null){
return 0;
} int [] res = new int[]{0};
dfs(root, root.val, 1, res);
return res[0];
} private void dfs(TreeNode root, int target, int count, int [] res){
if(root == null){
return;
} if(root.val != target+1){
count = 1;
}else{
count++;
} res[0] = Math.max(res[0], count);
dfs(root.left, root.val, count, res);
dfs(root.right, root.val, count, res);
}
}
跟上Binary Tree Longest Consecutive Sequence II.
类似Longest Consecutive Sequence.
LeetCode 298. Binary Tree Longest Consecutive Sequence的更多相关文章
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LC] 298. Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
Given a k-ary tree, find the length of the longest consecutive sequence path. The path could be star ...
随机推荐
- Sourse Insight使用教程及常见的问题解决办法
1.下载安装 2.创建项目new project(注意不是file-->new ),而是project-->new project,输入项目名称和密码. 3.添加文件,其实就是将你的整个项 ...
- 020_自己编写的wordcount程序在hadoop上面运行,不使用插件hadoop-eclipse-plugin-1.2.1.jar
1.Eclipse中无插件运行MP程序 1)在Eclipse中编写MapReduce程序 2)打包成jar包 3)使用FTP工具,上传jar到hadoop 集群环境 4)运行 2.具体步骤 说明:该程 ...
- sed实例
删除:d命令 $ sed '2d' example-----删除example文件的第二行. $ sed '2,$d' example-----删除example文件的第二行到末尾所有行. $ sed ...
- 【Tech】mac下svn和scp使用笔记
1.命令行从svn下载代码 mac本身自带svn,所以使用非常简单,在本地创建代码存放的文件夹,然后cd到该文件夹下,运行: svn checkout svn://ip地址/文件路径 . 然后出现要求 ...
- Qt5.4.1移植到arm——Linuxfb篇
Qt5与Qt4对比有很大的改变,其最大的特性在于模块化,并且很明显的是不再见到Qt4用到的qws,Qt5新增了QPA系统,基于QPA使得Qt5移 植到一个新平台非常简单而又具有极强的底层扩展能力:同时 ...
- Python多线程循环
背景:Python脚本:读取文件中每行,放入列表中:循环读取列表中的每个元素,并做处理操作. 核心:多线程处理单个for循环函数调用 模块:threading 第一部分: :多线程脚本 (该脚本只 ...
- java入门了解04
1.异常的体系:---------|Throwable --------------| Error (错误) 错误一般是由于jvm或者是硬件引发的问题,所以我们一般都不会通过代码去处理.------- ...
- iOS APP AppIcon& LaunchImage
AppIcon size for iPhone: 29 - Settings @1x 29*29, 58 - Settings @2x 58*58, 87 - Settings @3x 87*87 ...
- spark学习1(hadoop集群搭建)
把原先搭建的集群环境给删除了,自己重新搭建了一次,将笔记整理在这里,方便自己以后查看 第一步:安装主节点spark1 第一个节点:centos虚拟机安装,全名spark1,用户名hadoop,密码12 ...
- HBase-修改表结构
HBase修改表结构 package com.hbase.HBaseAdmin; import java.io.IOException; import org.apache.hadoop.conf.C ...