给你一棵指定的二叉树的根节点 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
DFS。
# 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: TreeNode) -> int:
def dfs(root, p, t):
nonlocal ans
if root is None:
return
t = t + 1 if p is not None and p.val + 1 == root.val else 1
ans = max(ans, t)
dfs(root.left, root, t)
dfs(root.right, root, t)
ans = 1
dfs(root, None, 1)
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) {
ans = 1;
dfs(root, null, 1);
return ans;
}
private void dfs(TreeNode root, TreeNode p, int t) {
if (root == null) {
return;
}
t = p != null && p.val + 1 == root.val ? t + 1 : 1;
ans = Math.max(ans, t);
dfs(root.left, root, t);
dfs(root.right, root, 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 ans;
int longestConsecutive(TreeNode* root) {
ans = 1;
dfs(root, nullptr, 1);
return ans;
}
void dfs(TreeNode* root, TreeNode* p, int t) {
if (!root) return;
t = p != nullptr && p->val + 1 == root->val ? t + 1 : 1;
ans = max(ans, t);
dfs(root->left, root, t);
dfs(root->right, root, t);
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func longestConsecutive(root *TreeNode) int {
ans := 1
var dfs func(root, p *TreeNode, t int)
dfs = func(root, p *TreeNode, t int) {
if root == nil {
return
}
if p != nil && p.Val+1 == root.Val {
t++
ans = max(ans, t)
} else {
t = 1
}
dfs(root.Left, root, t)
dfs(root.Right, root, t)
}
dfs(root, nil, 1)
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}