comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
中等 |
|
给你一棵指定的二叉树的根节点 root
,请你计算其中 最长连续序列路径 的长度。
最长连续序列路径 是依次递增 1 的路径。该路径,可以是从某个初始节点到树中任意节点,通过「父 - 子」关系连接而产生的任意路径。且必须从父节点到子节点,反过来是不可以的。
示例 1:
输入:root = [1,null,3,2,4,null,null,null,5] 输出:3 解释:当中,最长连续序列是3-4-5 ,所以
返回结果为3 。
示例 2:
输入:root = [2,null,3,2,null,1] 输出:2 解释:当中,最长连续序列是2-3 。注意,不是
3-2-1,所以
返回2 。
提示:
- 树中节点的数目在范围
[1, 3 * 104]
内 -3 * 104 <= Node.val <= 3 * 104
我们设计一个函数
函数
如果
否则,我们递归计算
然后我们更新答案为
最后,我们调用
时间复杂度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
def dfs(root: Optional[TreeNode]) -> int:
if root is None:
return 0
l = dfs(root.left) + 1
r = dfs(root.right) + 1
if root.left and root.left.val - root.val != 1:
l = 1
if root.right and root.right.val - root.val != 1:
r = 1
t = max(l, r)
nonlocal ans
ans = max(ans, t)
return t
ans = 0
dfs(root)
return ans
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int ans;
public int longestConsecutive(TreeNode root) {
dfs(root);
return ans;
}
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.left.val - root.val != 1) {
l = 1;
}
if (root.right != null && root.right.val - root.val != 1) {
r = 1;
}
int t = Math.max(l, r);
ans = Math.max(ans, t);
return t;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int longestConsecutive(TreeNode* root) {
int ans = 0;
function<int(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return 0;
}
int l = dfs(root->left) + 1;
int r = dfs(root->right) + 1;
if (root->left && root->left->val - root->val != 1) {
l = 1;
}
if (root->right && root->right->val - root->val != 1) {
r = 1;
}
int t = max(l, r);
ans = max(ans, t);
return t;
};
dfs(root);
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func longestConsecutive(root *TreeNode) (ans int) {
var dfs func(*TreeNode) int
dfs = func(root *TreeNode) int {
if root == nil {
return 0
}
l := dfs(root.Left) + 1
r := dfs(root.Right) + 1
if root.Left != nil && root.Left.Val-root.Val != 1 {
l = 1
}
if root.Right != nil && root.Right.Val-root.Val != 1 {
r = 1
}
t := max(l, r)
ans = max(ans, t)
return t
}
dfs(root)
return
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function longestConsecutive(root: TreeNode | null): number {
let ans = 0;
const dfs = (root: TreeNode | null): number => {
if (root === null) {
return 0;
}
let l = dfs(root.left) + 1;
let r = dfs(root.right) + 1;
if (root.left && root.left.val - root.val !== 1) {
l = 1;
}
if (root.right && root.right.val - root.val !== 1) {
r = 1;
}
const t = Math.max(l, r);
ans = Math.max(ans, t);
return t;
};
dfs(root);
return ans;
}