首页 > 编程语言 > 详细

二叉树的深度-python

时间:2019-08-10 12:01:48      阅读:91      评论:0      收藏: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
        left = self.TreeDepth(pRoot.left)
        right = self.TreeDepth(pRoot.right)
        return max(left, right) + 1

二叉树的深度-python

原文:https://www.cnblogs.com/dolisun/p/11330804.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!