Skip to content

Commit 18a6f07

Browse files
author
Nicholas
committed
updated solution to be more concise
1 parent 533bf55 commit 18a6f07

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

swift/1448-Count-Good-Nodes-In-Binary-Tree.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ class Solution {
1111

1212
func helper(_ root: TreeNode?, _ lastVal: Int) -> Int {
1313
guard let root = root else { return 0 }
14-
15-
if root.val >= lastVal {
16-
var left = helper(root.left, max(lastVal, root.val))
17-
var right = helper(root.right, max(lastVal, root.val))
18-
return 1 + left + right
19-
} else {
20-
var left = helper(root.left, max(lastVal, root.val))
21-
var right = helper(root.right, max(lastVal, root.val))
22-
return left + right
23-
}
14+
let i = root.val >= lastVal ? 1 : 0
15+
let left = helper(root.left, max(lastVal, root.val))
16+
let right = helper(root.right, max(lastVal, root.val))
17+
return i + left + right
2418
}
2519
}

0 commit comments

Comments
 (0)