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

    原题链接在这里:https://leetcode.com/problems/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.

    题解:

    DFS bottom-up方法. 

    Time Complexity: O(n). Space: O(logn).

    AC Java:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     int max = 0;
    12     public int longestConsecutive(TreeNode root) {
    13         dfs(root);
    14         return max;
    15     }
    16     
    17     private int dfs(TreeNode root){
    18         if(root == null){
    19             return 0;
    20         }
    21         
    22         int l = dfs(root.left)+1;
    23         int r = dfs(root.right)+1;
    24         if(root.left != null && root.val != root.left.val-1){
    25             l = 1;
    26         }
    27         
    28         if(root.right != null && root.val != root.right.val-1){
    29             r = 1;
    30         }
    31         
    32         int curMax = Math.max(l, r);
    33         max = Math.max(max, curMax);
    34         return curMax;
    35     }
    36 }

    也可采用top down的方法. 

    Time Complexity: O(n). Space: O(logn).

    AC Java:

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

    跟上Binary Tree Longest Consecutive Sequence II.

    类似Longest Consecutive Sequence.

  • 相关阅读:
    webstorm创建和搭建vue项目
    win10企业版激活方法
    Jquery使用规范(一)
    Json序列化循环引用的问题
    后台调用存储过程
    将金额人民币转化为大写 C#
    谷歌分辨率扩展程序的添加
    MVC 删除文件
    MVC C# 调用存储过程
    XML 转换 lits<>
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5234478.html
Copyright © 2011-2022 走看看