104 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

/**
* 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 max(maxDepth(root.Left),maxDepth(root.Right))+1
}
func max(a,b int)int {
if a>b{
return a
}
return b
}

本文介绍了一种计算二叉树最大深度的算法实现。通过递归方式遍历左右子树,找到从根节点到最远叶子节点的最长路径上的节点数。
881

被折叠的 条评论
为什么被折叠?



