leetcode Binary Tree Paths python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
self.res=[]
if root == None:
return self.res
def dfs(root,path):
if root.left is None and root.right is None:
self.res.append(path)
if root.left:
dfs(root.left,path+'->'+str(root.left.val))
if root.right:
dfs(root.right,path+'->'+str(root.right.val))
dfs(root,str(root.val))
return self.res
leetcode Binary Tree Paths python的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- LeetCode Binary Tree Paths(简单题)
题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LeetCode_257. Binary Tree Paths
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
随机推荐
- spark基本概念
Client:客户端进程,负责提交作业到Master. Application:Spark Application的概念和Hadoop MapReduce中的类似,指的是用户编写的Spark应用程序, ...
- mysql数据库的物理文件结构
mysql两种常用存储引擎myisam和innodb myisam不支持事务:innodb支持事务,当前作为插件来安装 myisam的数据库的物理文件结构为: .frm文件:与表相关的元数据信息都存放 ...
- 引用枚举进行对比时 enum需强制转换
枚举类 public enum MailRead { /// <summary> /// 未读 /// </summary> UNREAD=0, /// <summary ...
- Google Map 根据坐标 获取地址信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- 查看当前支持的MySQL字符集的命令
查看不同的MySQL字符集有不同的方法,下面介绍的命令用于查看当前支持的MySQL字符集,希望对您学习MySQL字符集能有所帮助. mysql> show char set; +-------- ...
- hibernate初体验
简介: Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate可以应用在任何使 ...
- blog开篇
本来是写java学习开篇的,现在就把它改为博客开篇吧. 其实一直都想着记录一下自己学习的过程,或者说是借口,一直在忙,也从重庆辗转到广州,又从广州辗转到天津了,又一个新阶段开始了,猴年马月都到了,哈哈 ...
- Qt中OpenGL的初步使用
结果预览: 一.代码5个文件 //glwidget.h #ifndef GLWIDGET_H #define GLWIDGET_H #include <QGLWidget> class G ...
- setTimeout的若干坑
第一坑:作用域 首先,有一个关于this的面试题,是这样的: var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: ...
- html5 canvas 画hello ketty
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...