码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode——543. 二叉树的直径

时间:2019-11-22 23:33:47      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:das   def   tin   tco   python3   self   inf   dia   tree   

似懂非懂。。

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

leetcode——543. 二叉树的直径

标签:das   def   tin   tco   python3   self   inf   dia   tree   

原文地址:https://www.cnblogs.com/taoyuxin/p/11914725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!