zoukankan      html  css  js  c++  java
  • 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.使用队列可以更加快速

  • 相关阅读:
    Window 服务安装
    SQL
    vb To c# or c# To vb
    CacheHelper-缓存
    让Linq的OrderBy支持动态字段
    哪些列上适合建立索引???
    如何查看Oracle数据库中无效的对象,约束,触发器和索引
    如何查看Oracle中被锁定的表
    ORACLE ASM
    WebLogic Server
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10169426.html
Copyright © 2011-2022 走看看