LeetCode 298. 二叉树最长连续序列(自顶向下)

文章目录

1. 题目

给你一棵指定的二叉树,请你计算它最长连续序列路径的长度。

该路径,可以是从某个初始结点到树中任意结点,通过「父 - 子」关系连接而产生的任意路径。

这个最长连续的路径,必须从父结点到子结点,反过来是不可以的。

示例 1:
输入:

   1
    \
     3
    / \
   2   4
        \
         5

输出: 3
解析: 当中,最长连续序列是 3-4-5,所以返回结果为 3

示例 2:
输入:

   2
    \
     3
    / 
   2    
  / 
 1

输出: 2 
解析: 当中,最长连续序列是 2-3。注意,不是 3-2-1,所以返回 2

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-longest-consecutive-sequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

class Solution {
	int maxlen = 1;
public:
    int longestConsecutive(TreeNode* root) {
    	if(!root) return 0;
    	dfs(root, 1);
    	return maxlen;
    }
    void dfs(TreeNode* root, int len)
    {
    	if(!root) return;
    	maxlen = max(maxlen, len);
    	if(root->left)
    	{
    		if(root->val+1 == root->left->val)
    			dfs(root->left, len+1);//满足连续,长度+1
    		else
    			dfs(root->left, 1);//不满足,从新开始
    	}
    	if(root->right)
    	{
    		if(root->val+1 == root->right->val)
    			dfs(root->right, len+1);
    		else
    			dfs(root->right, 1);
    	}
    }
};

68 ms 33.3 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章