【leetcode❤python】 257. Binary Tree Paths
深度优先搜索
# 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的更多相关文章
- [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 ...
- 【leetcode❤python】107. Binary Tree Level Order Traversal II
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】102. Binary Tree Level Order Traversal
#-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):# def ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- 【LeetCode OJ】Balanced Binary Tree
Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...
随机推荐
- 夺命雷公狗ThinkPHP项目之----企业网站25之网站前台面包屑导航URL的完善
如果想取出面包屑导航的url那么就必须在model层里面进行多取一个了: <?php namespace Home\Model; use Think\Model; class CategoryM ...
- BPM 应用系统开发案例实战
概述 IBM BPM 的前身是 Lombardi,是由 IBM 于 2009 年收购的产品,之后产品更名为 IBM WebSphere Lombardi Edition,目前最新版本称为 IBM BP ...
- APP的UI测试要点
1.文字显示是否正确 比如与需求图片对比是否正确,无错别字 2.对齐方式是否正确 3.图片 图片显示的篇幅不要太大. 4.颜色是否正确 颜色与需求规定的是否一致
- UIViewController启动过程
流程:判断是否view属性为nil,如果为空,调用loadView方法,如果重写了loadView方法,那么从代码创建普通视图,如果没有重写并且有storyBoard或者xib文件,那么从storyB ...
- Install DBD::mysql for Perl in XAMPP in Mac , solving errors
我不知道 why,在 Mac 安装 DBI::mysql 总会报错 我为了给 cgi-bin 添加 mysql-perl 数据库支持,也是够麻烦的 make sure that mysql and m ...
- ModalDialog.js
1. add <base target="_self" /> in the page of dialog, no need to use frame: <head ...
- spring.xml命名空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- Using Controls in a Form Design [AX 2012]
Using Controls in a Form Design [AX 2012] This topic has not yet been rated - Rate this topic Update ...
- Docker Centos安装Mysql5.6
之前一篇随笔<Docker Centos安装Openssh> 写的是如何在基础的centos镜像中搭建ssh服务,在此基础上再搭建其他服务.本文继续介绍在centos_ssh基础上搭建my ...
- ActiveMQ 安装异常
解决方式: 1.确认计算机主机名名称没有下划线: 2.如果是win7,停止ICS(运行-->services.msc找到Internet Connection Sharing (ICS)服务,改 ...