剑指Offerr37:二叉树的深度

思路:

运用递归的方法求出二叉树的高度。

递归求解:
假如是空节点,则返回0;
否则,原树的深度由左右子树中深度较的深度加1,为原树的深度。

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        if pRoot==None:
            return 0
        return max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right))+1

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/85229047
今日推荐