104 Maximum Depth of Binary Tree

题目来源:https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/
 
自我感觉难度/真实难度:easy/easy 

 真的是第一次完全没有看其他参考答案,第一次写出来,而且没有报错,值得庆祝一下。虽然题目很简单,但是我在使用递归时,还是害怕细节出错,ヾ(◍°∇°◍)ノ゙

 

题意:

求二叉树的高度

 
分析:

 只要递归下去就可以了

自己的代码:
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        temp_left=self.maxDepth(root.left)
        temp_right=self.maxDepth(root.right)
        return max(temp_left,temp_right)+1
代码效率/结果:
 Runtime: 72 ms, faster than 26.59% of Python3 online submissions forMaximum Depth of Binary Tree.
优秀代码:
   def maxDepth(self, root):
        return 1 + max(map(self.maxDepth, (root.left, root.right))) if root else 0
        

 

代码效率/结果:
 Runtime: 48 ms, faster than 97.78% of Python3 online submissions forMaximum Depth of Binary Tree.
自己优化后的代码:  

                 

 
反思改进策略:

      1.map函数的使用,可以简化代码、

  2.使用队列可以更加快速

 

转载于:https://www.cnblogs.com/captain-dl/p/10169426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值