LintCode 614. 二叉树的最长连续子序列 II

614. 二叉树的最长连续子序列 II

给定一棵二叉树,找到最长连续序列路径的长度。
路径起点跟终点可以为二叉树的任意节点。

样例
1
/ \
2 0
/
3
返回 4 // 0-1-2-3

思路:需要注意最后得到的结果必然由一半从下往上增加,另一半从下往上减小的两部分组成。所以对于每个节点都需要计算这两个值。因为需要返回两个值,所以自定义了一个类ReturnInfo,其中的longest_up表示以当前根节点为末节点从下往上增长的最长连续数量,longest_down类似,关键是要理解连续,以及在每个节点的处理细节.

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 
14 class ReturnInfo{
15 public:
16     int longest_up;
17     int longest_down;
18     ReturnInfo(int up,int down):longest_up(up),longest_down(down){};
19 }
20 
21 
22 class Solution {
23 public:
24     /**
25      * @param root: the root of binary tree
26      * @return: the length of the longest consecutive sequence path
27      */
28     int longestConsecutive2(TreeNode * root) {
29         // write your code here
30         longestInfo(root);
31         return longest;
32     }
33 
34     ReturnInfo longestInfo(TreeNode * root){
35         if(root == nullptr) return ReturnInfo(0,0);//nullptr返回(0,0)
36         int up = 1;//默认只有根节点,初始化为1
37         int down = 1;
38         ReturnInfo tempL = longestInfo(root->left);//左右节点的连续信息
39         ReturnInfo tempR = longestInfo(root->right);
40 
41         if(root->left){
42             if(root->left->val == root->val - 1) up = max(up,tempL.longest_up+1);//左子树是否连续进行更新
43             if(root->left->val == root->val + 1) down = max(down,tempL.longest_down+1);
44         }
45         
46         if(root->right){
47             if(root->right->val == root->val - 1) up = max(up,tempR.longest_up+1);//右子树处理
48             if(root->right->val == root->val + 1) down = max(down,tempR.longest_down+1);
49         }
50         
51         longest = max(longest,up+down-1);//以当前root为最高点的连续数字最多数量,仔细想想就是up+down-1。
52         return ReturnInfo(up,down);
53     }
54     
55 private:
56     int longest = 0;
57 };

 

转载于:https://www.cnblogs.com/J1ac/p/8846494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于一棵二叉树,我们可以定义其最长连续子序列(LCS)为从根节点开始,沿着任意路径向下遍历,使得经过的结点值连续递增或递减的最长路径长度。 为了求解二叉树的 LCS,我们可以使用递归的思想。具体来说,我们可以定义一个递归函数 dfs,其参数为当前结点和上一个结点的值 pre,并返回以当前结点为结尾的 LIS 长度和 LDS 长度。对于一个结点 node,如果 node.val = pre + 1,则可以将当前结点加入到 LIS 中;如果 node.val = pre - 1,则可以将当前结点加入到 LDS 中。如果 node.val 既不是 pre + 1 也不是 pre - 1,则需要重新开始计算 LCS 长度。最终的 LCS 长度即为所有以各个结点为结尾的 LIS 和 LDS 长度的最大值。 下面是一个 Java 代码实现: ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } class Solution { public int longestConsecutive(TreeNode root) { if (root == null) { return 0; } int[] res = dfs(root, root.val - 1); return Math.max(res[0], res[1]); } private int[] dfs(TreeNode node, int pre) { if (node == null) { return new int[]{0, 0}; } int[] left = dfs(node.left, node.val); int[] right = dfs(node.right, node.val); int[] cur = new int[]{1, 1}; if (node.val == pre + 1) { cur[0] = Math.max(cur[0], left[0] + 1); cur[1] = Math.max(cur[1], right[1] + 1); } else if (node.val == pre - 1) { cur[0] = Math.max(cur[0], right[0] + 1); cur[1] = Math.max(cur[1], left[1] + 1); } return cur; } } ``` 其中 dfs 函数返回一个长度为 2 的数组,第一个元素表示以当前结点为结尾的 LIS 长度,第二个元素表示以当前结点为结尾的 LDS 长度。对于每个结点,我们分别计算其向左子树和右子树的 LIS 和 LDS 长度,并根据当前结点与上一个结点的大小关系来更新当前结点的 LIS 和 LDS 长度。最终返回根节点的 LIS 和 LDS 长度的最大值即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值