Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Binary Tree Longest Consecutive Sequence

Posted on July 25, 2018July 26, 2020 by braindenny

Binary Tree Longest Consecutive Sequence



Similar Problems:

  • LeetCode: Binary Tree Longest Consecutive Sequence II
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #binarytree, #treetraversal, #classic

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).

Example 1:

Input:

   1
    \
     3
    / \
   2   4
        \
         5

Output: 3

Explanation: Longest consecutive sequence path is 3-4-5, so return 3.

Example 2:

Input:

   2
    \
     3
    / 
   2    
  / 
 1

Output: 2 

Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/binary-tree-longest-consecutive-sequence
// Basic Ideas: pre-order
//
// Complexity: Time O(n), Space O(1)
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func preOrder(root *TreeNode, maxCnt *int) int {
    if root == nil { return 0 }
    res := 1
    var count int
    if root.Left != nil {
        count = 1
        v := preOrder(root.Left, maxCnt)
        if root.Val + 1 == root.Left.Val {  count = v + 1 }
        if count > res { res = count }
    }
    if root.Right != nil {
        count = 1
        v := preOrder(root.Right, maxCnt)
        if root.Val + 1 == root.Right.Val {  count = v + 1 }
        if count > res { res = count }
    }
    if res > *maxCnt { *maxCnt = res }
    return res
}

func longestConsecutive(root *TreeNode) int {
    maxCnt := 0
    preOrder(root, &maxCnt)
    return maxCnt
}
linkedin
github
slack

Post Views: 4
Posted in MediumTagged #binarytree, #classic, treetraversal

Post navigation

LeetCode: Largest BST Subtree
LeetCode: Binary Tree Longest Consecutive Sequence II

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.