zoukankan      html  css  js  c++  java
  • 257. Binary Tree Paths

    题目来源:
     https://leetcode.com/problems/binary-tree-paths/
    自我感觉难度/真实难度:easy
    题意:
     
    分析:
     
    自己的代码:
    class Solution:
        def binaryTreePaths(self, root):
            """
            :type root: TreeNode
            :rtype: List[str]
            """
            all_re=[]
            res=''
            return self.dsf(root,all_re)
            
        def dsf(self,root,all_res):
            if not root.right and not root.left:
                return all_res.append(res[2:])                                                                                
            if root.left:
                string='->%d'%root.left.val
                res+=string
                dsf(root.left)
            if root.right:
                string='->%d'%root.left.val
                res+=string
                dsf(root.right)
            return all_res
                
    代码效率/结果:
     
    优秀代码:
    class Solution:
        
        r_list = []
        def __init__(self):
            self.r_list = []
        def binaryTreePaths(self, root):
            """
            :type root: TreeNode
            :rtype: List[str]
            """
            self.__init__()
            if not root:
                return self.r_list
            if (not root.left and not root.right):
                self.r_list.append(str(root.val))
            if root.left:
                self.recur_node(root.left, str(root.val))
            if root.right:
                self.recur_node(root.right, str(root.val))
            return self.r_list
        
        def recur_node(self, point, current_string):
            """
            type point: TreeNode
            retun type: void
            """
            r_string = current_string + "->" + str(point.val)
            if (not point.left and not point.right):
                self.r_list.append(r_string)
                return
            
            if point.left:
                self.recur_node(point.left, r_string)
            if point.right:
                self.recur_node(point.right, r_string)
                
            return

    全局变量也可以写在solution下面

    class Solution:
        def binaryTreePaths(self, root):
            """
            :type root: TreeNode
            :rtype: List[str]
            """
            if not root:
                return []
            
            paths = []
            stack = [(root, str(root.val))]
            while stack:
                node, path = stack.pop()
                if not node.left and not node.right:
                    paths.append(path)
                if node.left:
                    stack.append((node.left, path + '->' + str(node.left.val)))
                if node.right:
                    stack.append((node.right, path + '->' + str(node.right.val)))
            
            return paths

    循环的做法

    class Solution:
        def binaryTreePaths(self, root):
            """
            :type root: TreeNode
            :rtype: List[str]
            """
            def dsf(root,path):
                if root:
                    path += str(root.val)
                    if not root.right and not root.left:
                        paths.append(path)
                    else:
                        path+='->'
                        dsf(root.right,path)
                        dsf(root.left,path)
            paths=[]
            dsf(root,'')
            return paths

    迭代,把函数写在函数里面

    class Solution:
        # @param {TreeNode} root
        # @return {string[]}
        def binaryTreePaths(self, root):
            res, path_list = [], []
            self.dfs(root, path_list, res)
            return res
    
        def dfs(self, root, path_list, res):
            if not root:
                return
            path_list.append(str(root.val))
            if not root.left and not root.right:
                res.append('->'.join(path_list))
            if root.left:
                self.dfs(root.left, path_list, res)
            if root.right:
                self.dfs(root.right, path_list, res)
            path_list.pop()

    函数并列假设,要想改变他的值,一路带着走

    代码效率/结果:
     
    自己优化后的代码:
     
    反思改进策略:

    1.对递归使用的情况不熟悉,特别是这种有全局变量和局部变量的,不知道怎么处理

    2.

    写题时间时长:
  • 相关阅读:
    .NET WinForm 状态栏添加分隔符
    使用 MMC / IE 查看证书
    配置IIS在64位Windows上运行 32 位 ASP.NET 应用程序
    WCF部署到IIS:证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限
    PB6 调用 .net Web Service
    .NET程序加壳 — 之动态加载程序集
    statusStrip 状态条 toolStripStatusLabel 居右显示
    C# 使用 Stopwatch 测量代码运行时间
    Application_Start 事件中使用 Response.Redirect
    解决IIS中部署WCF时,访问.svc文件的404错误问题
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10340113.html
Copyright © 2011-2022 走看看