题解 | #二叉树的深度#
二叉树的深度
https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return int整型
#
class Solution:
def TreeDepth(self , pRoot: TreeNode) -> int:
# write code here
# 先判空节点
if not pRoot:
return 0
return max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right)) + 1
- 树经常和递归结合,要注意递归结束条件,和递归的入口
剑指offer刷题笔记 文章被收录于专栏
24秋招剑指offer刷题的笔记

查看30道真题和解析
