算法题---递归求二叉树的深度

 

递归过程

https://www.bilibili.com/video/BV1Nt411K75B?from=search&seid=3890139359243729354

 

 

class TreeNode(object):
    def __init__(self, x):
        self.left = None
        self.right = None
        self.val = x


def treeDeep(root):
    if root == None:
        return 0
    left = treeDeep(root.left)
    right = treeDeep(root.right)
    return left + 1 if left > right else right + 1

 

posted @ 2020-10-28 15:45  威威后花园  阅读(211)  评论(0编辑  收藏  举报