Skip to content

Commit 4bff89d

Browse files
committed
0298-binary-tree-longest-consecutive-sequence
1 parent 40a05bf commit 4bff89d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// 298. Binary Tree Longest Consecutive Sequence
2+
/// Given a binary tree, find the length of the longest consecutive sequence path. The path refers
3+
/// to any sequence of nodes from some starting node to any node in the tree along the parent-child
4+
/// connections. The longest consecutive path need to be from parent to child (cannot be the
5+
/// reverse).
6+
7+
import XCTest
8+
9+
final class TreeNode {
10+
var val: Int
11+
var left: TreeNode?
12+
var right: TreeNode?
13+
init(_ val: Int) {
14+
self.val = val
15+
self.left = nil
16+
self.right = nil
17+
}
18+
}
19+
20+
/// Approach: DFS
21+
func longestConsecutive(_ root: TreeNode?) -> Int {
22+
var maxLength = 0
23+
func dfs(_ node: TreeNode?) -> Int {
24+
if let node = node {
25+
var leftLength = dfs(node.left) + 1
26+
var rightLength = dfs(node.right) + 1
27+
if let left = node.left,
28+
node.val + 1 != left.val {
29+
leftLength = 1
30+
}
31+
32+
if let right = node.right,
33+
node.val + 1 != right.val {
34+
rightLength = 1
35+
}
36+
let length = max(leftLength, rightLength)
37+
maxLength = max(length, maxLength)
38+
return length
39+
} else {
40+
return 0
41+
}
42+
}
43+
dfs(root)
44+
return maxLength
45+
}
46+
47+
class Tests: XCTestCase {
48+
func testExample() {
49+
// TODO: Add test case
50+
}
51+
}
52+
53+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ You can visit the pages below to search problems by company tags. Then come back
5858
200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0200-number-of-islands.playground/Contents.swift) | BFS
5959
259 | [Three Sum Smaller](https://leetcode.com/problems/3sum-smaller/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0259-3sum-smaller.playground/Contents.swift) | Two Pointers
6060
280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0280-wiggle-sort.playground/Contents.swift) | Swap
61+
298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0298-binary-tree-longest-consecutive-sequence.playground/Contents.swift) | DFS
6162
482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift) | String
6263
681 | [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift) | Simulation
6364
683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift) | Sliding Window

0 commit comments

Comments
 (0)