Maximum Depth of Binary Tree

Solution in Go

DFS

func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }

    left := maxDepth(root.Left) + 1
    right := maxDepth(root.Right) + 1

    if left > right {
        return left
    }

    return right
}
Subscribe via email

Get notified once/twice per month when new articles are published.

Copyright © 2022 - 2023 TheDeveloperCafe.
The Go gopher was designed by Renee French.