Post

Leetcode - 104. Maximum Depth of Binary Tree

Leetcode - 104. Maximum Depth of Binary Tree

Hits

  • Given the root of a binary tree, return its maximum depth.

    A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func maxDepth(root *TreeNode) int {
    if root == nil{
        return 0
    }

    leftDepth := maxDepth(root.Left)
    rightDepth := maxDepth(root.Right)

    if leftDepth>rightDepth{
        return leftDepth+1
    }
    return rightDepth+1
    
}
This post is licensed under CC BY 4.0 by the author.