[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:
- Input:
- 1
- / \
- 2 3
- \
- 5
- Output: ["1->2->5", "1->3"]
- Explanation: All root-to-leaf paths are: 1->2->5, 1->3
- Iteration:
- # 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]
- """
- if not root:
- return []
- stack=[]
- stack.append((root,str(root.val)))
- paths=[]
- while stack:
- node,path=stack.pop()
- if not node.left and not node.right:
- paths.append(path)
- else:
- 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
Recursion:
- # 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]
- """
- ans=[]
- def isleaf(node):
- if not node.right and not node.left:
- return True
- return False
- def findPath(node,path):
- if node:
- path+=str(node.val)
- if isleaf(node):
- ans.append(path)
- else:
- path+="->"
- findPath(node.left,path)
- findPath(node.right,path)
- path=""
- findPath(root,path)
- return ans
[LeetCode&Python] Problem 257. Binary Tree Paths的更多相关文章
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- 【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 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- vim中^M的研究
vim打开文件时在行尾显示^M,这样的情况时不时会遇到,下面稍微深入了解下这个问题: 原理呢,其实很简单:Windows换行风格(也叫dos风格)的文本以Unix风格解析就会出现这个情况: 首先重现这 ...
- 【题解】Luogu P2057 [SHOI2007]善意的投票
原题传送门 我们一眼就能看出这是一道最小割的题 我们设不睡觉这种状态为S,睡觉这种状态为T 对于每个人,如果不想睡觉,就从S向这个人连流量为1的边,否则,就从这个人向T连流量为1的边 对于每一对朋友, ...
- Ubuntu 16.04下vsftpd 安装配置实例
从https://www.linuxidc.com/Linux/2017-06/144807.htm转载 第一步:安装VSFTPD sudo apt-get install vsftpd 安装完成后启 ...
- 牛客-数据库SQL实战
查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` ) NOT NULL, `birth_date` date NOT NULL, `first_nam ...
- 浅谈JS数据遍历的几种方式
遍历对象(数组)是我们日常撸码的必不可少的部分,如何从性能上优化代码,提高运行效率?下文为你揭开真像: 第一种:普通的for循环 for(j = 0; j < arr.length; j++) ...
- 使用scss为css样式自动添加浏览器前缀
当一个浏览器实现一个新的属性.值或者选择器,而这个特征还不是处于候选推荐标准状态的时候,这属性的前面会添加一个前缀以便于它的渲染引擎识别. 浏览器使用前缀来尝试一些新属性.值和选择器,即使他们还没有最 ...
- spring 配置Value常量(不支持到static上)
spring 配置Value常量(不支持到static上) 看代码吧,语言表达有问题. package com.variflight.xzair.rest.constant; import org.s ...
- 虚拟环境之virtualenvwrapper
原来的virtualenv工具使用特别麻烦,主要体现在以下几点 1 创建虚拟环境的命令太长,太难记 2 管理特别麻烦 3 进入虚拟环境需要找到这个虚拟环境的存放目录才行,如果没有统一的存放目录,很难找 ...
- php 中swoole安装
1.下载扩展包: 网址:http://pecl.php.net/ 中(http://pecl.php.net/get/swoole-4.3.1.tgz) 2.解压安装包. 3.进入解压好的安装包. 4 ...
- 详细解析HTML基础结构
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...