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 }

AC Python:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def longestConsecutive(self, root: Optional[TreeNode]) -> int:
 9         self.res = 0
10         self.dfs(root)
11         return self.res
12     
13     def dfs(self, root):
14         if not root:
15             return 0
16         
17         l = self.dfs(root.left) + 1
18         r = self.dfs(root.right) + 1
19         if root.left and root.left.val != root.val + 1:
20             l = 1
21         if root.right and root.right.val != root.val + 1:
22             r = 1
23         
24         curMax = max(l, r)
25         self.res = max(self.res, curMax)
26         return curMax

也可采用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.

posted @ 2016-03-02 12:46  Dylan_Java_NYC  阅读(474)  评论(0编辑  收藏  举报