zoukankan      html  css  js  c++  java
  • [LeetCode] 298. Binary Tree Longest Consecutive Sequence

    Given the root of a binary tree, return 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 needs to be from parent to child (cannot be the reverse).

    Example 1:

    Input: root = [1,null,3,2,4,null,null,null,5]
    Output: 3
    Explanation: Longest consecutive sequence path is 3-4-5, so return 3.
    

    Example 2:

    Input: root = [2,null,3,2,null,1]
    Output: 2
    Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.

    Constraints:

    • The number of nodes in the tree is in the range [1, 3 * 104].
    • -3 * 104 <= Node.val <= 3 * 104

    二叉树最长连续序列。

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

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

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

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

    时间O(n)

    空间O(n)

    JavaScript实现

     1 /**
     2  * @param {TreeNode} root
     3  * @return {number}
     4  */
     5 
     6 var longestConsecutive = function(root) {
     7     if (root === null) return 0;
     8     return helper(root);
     9 };
    10 
    11 var helper = function(node, prev, count) {
    12     if (!node) {
    13         return count;
    14     }
    15     if (node.val === prev + 1) {
    16         count++;
    17     } else {
    18         count = 1;
    19     }
    20     let res = Math.max(
    21         count,
    22         helper(node.left, node.val, count),
    23         helper(node.right, node.val, count)
    24     );
    25     return res;
    26 };

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode() {}
     8  *     TreeNode(int val) { this.val = val; }
     9  *     TreeNode(int val, TreeNode left, TreeNode right) {
    10  *         this.val = val;
    11  *         this.left = left;
    12  *         this.right = right;
    13  *     }
    14  * }
    15  */
    16 class Solution {
    17     int res = 0;
    18 
    19     public int longestConsecutive(TreeNode root) {
    20         if (root == null) {
    21             return 0;
    22         }
    23         helper(root, 0, root.val);
    24         return res;
    25     }
    26 
    27     private void helper(TreeNode root, int max, int target) {
    28         if (root == null) {
    29             return;
    30         }
    31         if (root.val == target) {
    32             max++;
    33         } else {
    34             max = 1;
    35         }
    36         res = Math.max(res, max);
    37         helper(root.left, max, root.val + 1);
    38         helper(root.right, max, root.val + 1);
    39     }
    40 }

    LeetCode 题目总结

  • 相关阅读:
    【TJOI2015】弦论 (后缀数组)
    再见,高中;你好,大学
    我亲爱的朋友们
    将容斥系数隐含在式子中的方法
    一个奇妙的斯特林数推导
    CSP2019游记
    CSP2019初赛游记
    NOI2019游记
    老年选手康复训练
    CTS/APIO2019 游记
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12186848.html
Copyright © 2011-2022 走看看