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.

分析:https://segmentfault.com/a/1190000003957798

递归法

复杂度

时间O(n) 空间O(h)

思路

因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径有多长,二是目前路径的上一个节点的值。我们通过递归把这些信息代入,然后通过返回值返回一个最大的就行了。这种需要遍历二叉树,然后又需要之前信息的题目思路都差不多,比如Maximum Depth of Binary TreeBinary Tree Maximum Path Sum

 1 public class Solution {
 2     public int longestConsecutive(TreeNode root) {
 3         if(root == null){
 4             return 0;
 5         }
 6         return findLongest(root, 0, root.val - 1);
 7     }
 8
 9     private int findLongest(TreeNode root, int length, int preVal){
10         if(root == null){
11             return length;
12         }
13         // 判断当前是否连续
14         int currLen = preVal + 1 == root.val ? length + 1 : 1;
15         // 返回当前长度,左子树长度,和右子树长度中较大的那个
16         return Math.max(currLen, Math.max(findLongest(root.left, currLen, root.val), findLongest(root.right, currLen, root.val)));
17     }
18 }

另一种解法:http://www.cnblogs.com/grandyang/p/5252599.html

这道题让我们求二叉树的最长连续序列,关于二叉树的题基本都需要遍历树,而递归遍历写起来特别简单,下面这种解法是用到了递归版的先序遍历,我们对于每个遍历到的节点,我们看节点值是否比参数值(父节点值)大1,如果是则长度加1,否则长度重置为1,然后更新结果res,再递归调用左右子节点即可,参见代码如下:

 1 class Solution {
 2 public:
 3     int longestConsecutive(TreeNode* root) {
 4         if (!root) return 0;
 5         int res = 0;
 6         dfs(root, root->val - 1, 0, res);
 7         return res;
 8     }
 9     void dfs(TreeNode *root, int v, int out, int &res) {
10         if (!root) return;
11         if (root->val == v + 1) ++out;
12         else out = 1;
13         res = max(res, out);
14         dfs(root->left, root->val, out, res);
15         dfs(root->right, root->val, out, res);
16     }
17 };
时间: 2025-01-02 17:37:32

Binary Tree Longest Consecutive Sequence的相关文章

[Locked] Binary Tree Longest Consecutive Sequence

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 l

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 p

Leetcode: 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 p

298. 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 fr

Binary Tree Longest Consecutive Sequence II

Note: O(n) This question is the extension of the version one. The longest consecutive Sequence could be found in "Parent->Children" or "Children -> Parent -> Children". The first situation is the same as previous one. The seco

[LeetCode] 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 p

[LC] 298. 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 p

Binary Tree Longest Consecutive Sequence III

Note: This question is good summary for this kind of problem. 1) Once you get the root, loop through all the children. Get the max up/down/max from the children. 2) set root max = up + down + 1, then comparing with the max got from the children. 3) R

LeetCode 549. Binary Tree Longest Consecutive Sequence II

Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not v