深度优先搜索

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

class Solution:
    # @param {TreeNode} root
    # @return {string[]}
    
    resultList=[]
    def binaryTreePaths(self, root):
        self.resultList=[]
        if root==None:
            return []
        else:
            self.resultList.append(str(root.val))
            self.dfs(root)
       
        return self.resultList
        
    def dfs(self,root):
      
        curVal=self.resultList[-1]
       
        if root.left!=None or root.right!=None:
            self.resultList.pop()
        else:return
         
        if root.left!=None:
            val=str(curVal)+'->'+str(root.left.val)
            self.resultList.append(val)
            self.dfs(root.left)
        if root.right!=None:
            val=str(curVal)+'->'+str(root.right.val)
            self.resultList.append(val)
            self.dfs(root.right)

【leetcode❤python】 257. Binary Tree Paths的更多相关文章

  1. [LeetCode&Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. 【leetcode❤python】107. Binary Tree Level Order Traversal II

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  3. 【leetcode❤python】102. Binary Tree Level Order Traversal

    #-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):#     def ...

  4. <LeetCode OJ> 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  5. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  6. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  7. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  9. 【LeetCode OJ】Balanced Binary Tree

    Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...

随机推荐

  1. oracle的簇的管理

    使用ALTER修改簇属性(必须拥有ALTER ANY CLUSTER的权限) 1.修改簇属性 可以修改的簇属性包括: * PCTFREE.PCTUSED.INITRANS.MAXTRANS.STORA ...

  2. 一个容易被忽略的ReportingService超时问题

    我们在使用Sql Server Reporting Service开发报表的时候,经常会遇到报表超时的问题,报表超时的原因有很多,也有很多地方可以设置报表的超时时间,比如在报表中的数据源(dataso ...

  3. 1行代码,删除svn文件夹

    引用:http://www.cnblogs.com/Alexander-Lee/archive/2010/02/23/1671905.html linux操作系统: find -name .svn | ...

  4. SQL内连接与外连接的区别【转】

    --表stuid name 1, Jack2, Tom3, Kity4, nono--表examid grade1, 562, 7611, 89 内连接 (显示两表id匹配的)select stu.i ...

  5. 通过HP Loadrunner VuGen来录制安卓的应用

    作者:Richard Pal       来自:perftesting           翻译:Elaine00 通过这篇文章,我将介绍如何通过HP Loadrunner VuGen来测试一个安卓应 ...

  6. dir cmd、the DIR Command、windows

    原因   :如何在windows下的cmd.exe中只列出文件名? solve : dir \a:-d \b Extend Reading : dir [drive:][path][filename] ...

  7. tomcat监控脚本

    工作所需,匆匆忙忙写了个监控tomcat的shell脚本,大概思路是这样的:先检测tomcat进程是否存在,如果不存在就启动,如果进程存在,检测页面返回码状态,如果是200就是正常,如果不是就重启. ...

  8. Server.MapPath()获取本机绝对路径

    1.    Server.MapPath("/")  应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Server.MapPath("./&qu ...

  9. DomainHelper

    public class DomainHelper { public static void SetDomainValue(string key, object password) { AppDoma ...

  10. php开启openssl的方法

    windows下开启方法: 1: 首先检查php.ini中:extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘:’, 如果不存在这行,那么添加extensio ...