leetcode——543. 二叉树的直径

似懂非懂。。

class Solution:
    def diameterOfBinaryTree(self, root: TreeNode) -> int:
        self.ans=1
        def depth(node):
            if not node:
                return 0
            L=depth(node.left)
            R=depth(node.right)
            self.ans=max(self.ans,L+R+1)
            return max(L,R)+1
        depth(root)
        return self.ans-1
执行用时 :52 ms, 在所有 python3 提交中击败了95.04%的用户
内存消耗 :16.5 MB, 在所有 python3 提交中击败了5.10%的用户
 
——2019.11.22
我的前方是万里征途,星辰大海!!
原文地址:https://www.cnblogs.com/taoyuxin/p/11914725.html