Skip to content

Commit 4920929

Browse files
committed
Revert "112-Path-Sum JS & TS"
This reverts commit d3f2242.
1 parent 2d71a93 commit 4920929

File tree

3 files changed

+14
-120
lines changed

3 files changed

+14
-120
lines changed

javascript/112-Path-Sum.js

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,19 @@
1-
/**
2-
* Definition for a binary tree node.
3-
* public class TreeNode {
4-
* public var val: Int
5-
* public var left: TreeNode?
6-
* public var right: TreeNode?
7-
* public init() { self.val = 0; self.left = nil; self.right = nil; }
8-
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9-
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10-
* self.val = val
11-
* self.left = left
12-
* self.right = right
13-
* }
14-
* }
15-
*/
1+
//
2+
// 1448-Count-Good-Nodes-In-Binary-Tree.swift
3+
// Question Link: https://leetcode.com/problems/count-good-nodes-in-binary-tree/
4+
//
5+
166
class Solution {
17-
var globalCount: Int = 0
18-
197
func goodNodes(_ root: TreeNode?) -> Int {
20-
self.globalCount = 0
21-
visit(root, maxSoFar: nil)
22-
return self.globalCount
8+
guard let root = root else { return 0 }
9+
return helper(root, Int.min)
2310
}
24-
}
25-
26-
private extension Solution {
27-
// DFS/Recursive traversal
28-
func visit(_ node: TreeNode?, maxSoFar: Int?) -> Void {
29-
// Base case 1. Node is nil
30-
guard let node = node else { return }
31-
32-
// Base case 2. Initial value doesn't exist (in case of root)
33-
let maxVal: Int
34-
if let maxSoFar = maxSoFar {
35-
maxVal = max(maxSoFar, node.val)
36-
} else {
37-
maxVal = node.val
38-
}
39-
40-
// update global count
41-
if !(maxVal > node.val) {
42-
self.globalCount += 1
43-
}
44-
45-
// visit children
46-
visit(node.left, maxSoFar: maxVal)
47-
visit(node.right, maxSoFar: maxVal)
11+
12+
func helper(_ root: TreeNode?, _ lastVal: Int) -> Int {
13+
guard let root = root else { return 0 }
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
4818
}
4919
}
50-
51-

typescript/112-Path-Sum.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)