美文网首页
LeetCode-Golang之【104. 二叉树的最大深度】

LeetCode-Golang之【104. 二叉树的最大深度】

作者: StevenChu1125 | 来源:发表于2020-12-09 10:17 被阅读0次

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

题解

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    return 1 + max(maxDepth(root.Left),maxDepth(root.Right))
}

func max(x,y int) int {
    if x > y {
        return x
    }
    return y
}

相关文章

网友评论

      本文标题:LeetCode-Golang之【104. 二叉树的最大深度】

      本文链接:https://www.haomeiwen.com/subject/fupfgktx.html