Golang | DFS | tracking
func goodNodes(root *TreeNode) int {
    dfs := Dfs{}
    dfs.DfsSearch(root, root.Val)
    return dfs.Num
}

type Dfs struct {
    Num int
}

func (d *Dfs) DfsSearch(root *TreeNode, prevMax int) {
    if root == nil {
        return
    }
    curMax := prevMax
    if root.Val >= prevMax {
        curMax = root.Val
        d.Num++
    }
    d.DfsSearch(root.Left, curMax)
    d.DfsSearch(root.Right, curMax)
}
Comments (7)