Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

  1. Input:
  2.  
  3. 1
  4. / \
  5. 2 3
  6. \
  7. 5
  8.  
  9. Output: ["1->2->5", "1->3"]
  10.  
  11. Explanation: All root-to-leaf paths are: 1->2->5, 1->3
  12.  
  13. Iteration:
  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. def binaryTreePaths(self, root):
  10. """
  11. :type root: TreeNode
  12. :rtype: List[str]
  13. """
  14. if not root:
  15. return []
  16. stack=[]
  17. stack.append((root,str(root.val)))
  18. paths=[]
  19. while stack:
  20. node,path=stack.pop()
  21. if not node.left and not node.right:
  22. paths.append(path)
  23. else:
  24. if node.left:
  25. stack.append((node.left,path+"->"+str(node.left.val)))
  26. if node.right:
  27. stack.append((node.right,path+"->"+str(node.right.val)))
  28. return paths
  1.  

  

Recursion:

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. def binaryTreePaths(self, root):
  10. """
  11. :type root: TreeNode
  12. :rtype: List[str]
  13. """
  14. ans=[]
  15. def isleaf(node):
  16. if not node.right and not node.left:
  17. return True
  18. return False
  19. def findPath(node,path):
  20. if node:
  21. path+=str(node.val)
  22. if isleaf(node):
  23. ans.append(path)
  24. else:
  25. path+="->"
  26. findPath(node.left,path)
  27. findPath(node.right,path)
  28. path=""
  29. findPath(root,path)
  30. return ans

  

  1.  

[LeetCode&Python] Problem 257. Binary Tree Paths的更多相关文章

  1. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  2. <LeetCode OJ> 257. Binary Tree Paths

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

  3. [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 ...

  4. [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 ...

  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 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. LeetCode OJ 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. vim中^M的研究

    vim打开文件时在行尾显示^M,这样的情况时不时会遇到,下面稍微深入了解下这个问题: 原理呢,其实很简单:Windows换行风格(也叫dos风格)的文本以Unix风格解析就会出现这个情况: 首先重现这 ...

  2. 【题解】Luogu P2057 [SHOI2007]善意的投票

    原题传送门 我们一眼就能看出这是一道最小割的题 我们设不睡觉这种状态为S,睡觉这种状态为T 对于每个人,如果不想睡觉,就从S向这个人连流量为1的边,否则,就从这个人向T连流量为1的边 对于每一对朋友, ...

  3. Ubuntu 16.04下vsftpd 安装配置实例

    从https://www.linuxidc.com/Linux/2017-06/144807.htm转载 第一步:安装VSFTPD sudo apt-get install vsftpd 安装完成后启 ...

  4. 牛客-数据库SQL实战

    查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` ) NOT NULL, `birth_date` date NOT NULL, `first_nam ...

  5. 浅谈JS数据遍历的几种方式

    遍历对象(数组)是我们日常撸码的必不可少的部分,如何从性能上优化代码,提高运行效率?下文为你揭开真像: 第一种:普通的for循环 for(j = 0; j < arr.length; j++) ...

  6. 使用scss为css样式自动添加浏览器前缀

    当一个浏览器实现一个新的属性.值或者选择器,而这个特征还不是处于候选推荐标准状态的时候,这属性的前面会添加一个前缀以便于它的渲染引擎识别. 浏览器使用前缀来尝试一些新属性.值和选择器,即使他们还没有最 ...

  7. spring 配置Value常量(不支持到static上)

    spring 配置Value常量(不支持到static上) 看代码吧,语言表达有问题. package com.variflight.xzair.rest.constant; import org.s ...

  8. 虚拟环境之virtualenvwrapper

    原来的virtualenv工具使用特别麻烦,主要体现在以下几点 1 创建虚拟环境的命令太长,太难记 2 管理特别麻烦 3 进入虚拟环境需要找到这个虚拟环境的存放目录才行,如果没有统一的存放目录,很难找 ...

  9. php 中swoole安装

    1.下载扩展包: 网址:http://pecl.php.net/ 中(http://pecl.php.net/get/swoole-4.3.1.tgz) 2.解压安装包. 3.进入解压好的安装包. 4 ...

  10. 详细解析HTML基础结构

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...