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 parent to child (cannot be the reverse).
For example,
\ / \ \
Longest consecutive sequence path is 3-4-5
, so return 3
.
\ / /
Longest consecutive sequence path is 2-3
,not3-2-1
, so return 2
.
思路:递归。时间复杂度O(N)。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int help(TreeNode *root, int &count) {
if (root == NULL) return ;
int leftCount = help(root->left, count);
int rightCount = help(root->right, count);
int res = ;
if (root->left && root->val + == root->left->val)
res = + leftCount;
if (root->right && root->val + == root->right->val)
res = std::max(res, + rightCount);
count = std::max(count, res);
return res;
}
int longestConsecutive(TreeNode* root) {
int count = ;
help(root, count);
return count;
}
};
Binary Tree Longest Consecutive Sequence -- LeetCode的更多相关文章
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 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 ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [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 ...
- [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 ...
- [Locked] Binary Tree Longest Consecutive Sequence
Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- 代理设计模式iOS开发Demo(示例程序)源代码
iOS程序源代码下载链接:03-代理设计模式.zip28.3 KB // main.m // // main.m // 03-代理设计模式 // // Created by apple ...
- js_开发小技巧记录(一)
(一) 生成从minNum到maxNum的随机数 <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- 设计模式之笔记--建造者模式(Builder)
建造者模式(Builder) 定义 建造者模式(Builder),将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 类图 描述 Builder:定义一个建造者抽象类,以规范产 ...
- goreplay HTTP-HTTPS流量复制工具
goreplay相比tcpcopy只能复制HTTP和HTTPS的流量 goreplay编译很麻烦,就直接使用编译好的版本 gor_0.10.1_x64.tar.gz 支持centos5,测试的是cen ...
- Leetcode 之Longest Common Prefix(34)
这题实现起来还是挺麻烦的,就偷懒使用下库函数strtod().第二个参数表示字符中不是数字的地方,如果后面是空格,则认为其仍是数字,否则不是. bool isNumber(char *s) { cha ...
- NOIP 2013 day1
tags: 模拟 快速幂 逆序对 树状数组 归并排序 最小生成树 lca 倍增 categories: 信息学竞赛 总结 tex live 2017.iso 转圈游戏 火柴排队 货车运输 转圈游戏 s ...
- xshell 如何连接服务器
https://jingyan.baidu.com/article/ab69b270b0ca3d2ca7189fdc.html 点击“新建”之后就会出现下面这样一个界面,“名称”根据自己的需求填写,“ ...
- 二、ansible配置简要介绍
[defaults] # some basic default values… hostfile = /etc/ansible/hosts \\指定默认hosts配置的位置 # library_pat ...
- hdu 3371(kruskal)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 【转载】Python: Enum枚举的实现
转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html 从C系语言过来用Python,好不容易适应了写代码不打 ...