-
Notifications
You must be signed in to change notification settings - Fork 0
/
113-path-sum-2.swift
61 lines (50 loc) · 1.97 KB
/
113-path-sum-2.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
*
* Runtime: 48 ms, faster than 26.17% of Swift online submissions for Path Sum II.
* Memory Usage: 21.5 MB, less than 94.44% of Swift online submissions for Path Sum II.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func pathSum(_ root: TreeNode?, _ sum: Int) -> [[Int]] {
guard let root = root else { return [] }
var result = [[Int]]()
var stack = [(node: TreeNode, path: [Int], sum: Int)]()
stack.append((node: root, path: [root.val], sum: root.val))
while !stack.isEmpty {
let current = stack.removeLast()
let isLeaf = current.node.left == nil && current.node.right == nil
if isLeaf, current.sum == sum {
result.append(current.path)
continue
}
if let right = current.node.right {
let sum = current.sum + right.val
var path = current.path
path.append(right.val)
stack.append((node: right, path: path, sum: sum))
}
if let left = current.node.left {
let sum = current.sum + left.val
var path = current.path
path.append(left.val)
stack.append((node: left, path: path, sum: sum))
}
}
return result
}
}