forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1448-count-good-nodes-in-binary-tree.swift
50 lines (44 loc) · 1.37 KB
/
1448-count-good-nodes-in-binary-tree.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
/**
* 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 {
var globalCount: Int = 0
func goodNodes(_ root: TreeNode?) -> Int {
self.globalCount = 0
visit(root, maxSoFar: nil)
return self.globalCount
}
}
private extension Solution {
// DFS/Recursive traversal
func visit(_ node: TreeNode?, maxSoFar: Int?) -> Void {
// Base case 1. Node is nil
guard let node = node else { return }
// Base case 2. Initial value doesn't exist (in case of root)
let maxVal: Int
if let maxSoFar = maxSoFar {
maxVal = max(maxSoFar, node.val)
} else {
maxVal = node.val
}
// update global count
if !(maxVal > node.val) {
self.globalCount += 1
}
// visit children
visit(node.left, maxSoFar: maxVal)
visit(node.right, maxSoFar: maxVal)
}
}