Binary Tree Paths(leetcode 257)
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
Input:1/ \
2 3\5Output: ["1->2->5", "1->3"]Explanation: All root-to-leaf paths are: 1->2->5, 1->3
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入:
1/ \
2 3\5输出: ["1->2->5", "1->3"]解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def binaryTreePaths(self, root):""":type root: TreeNode:rtype: List[str]"""def construct_paths(root, path):if root:path += str(root.val)if not root.left and not root.right: # 当前节点是叶子节点paths.append(path) # 把路径加入到答案中else:path += '->' # 当前节点不是叶子节点,继续递归遍历construct_paths(root.left, path)construct_paths(root.right, path)paths = []construct_paths(root, '')return paths
Binary Tree Paths(leetcode 257)相关推荐
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1/ ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- 814. Binary Tree Pruning(C语言)
814. Binary Tree Pruning(C语言) 深度优先搜索树 + 剪枝 题目 Given the root of a binary tree, return the same tree ...
- CF750G New Year and Binary Tree Paths(数位dp二进制+数学)
CF750G New Year and Binary Tree Paths description solution code description 题目链接 一颗无穷个节点的完全二叉树. 求有多少 ...
- 二叉树的所有路径(LeetCode 257)
力扣 思路 : 1.递归 递归函数声明: vector<string> binaryTreePaths(TreeNode* root) : 递归出口: (1)如果根节点为空,返回 &q ...
- java binarytreenode_LeetCode算法题-Binary Tree Paths(Java实现-3种解法)
这是悦乐书的第199次更新,第206篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第62题(顺位题号是257).给定二叉树,返回所有根到叶路径.例如: 输入: 1 / \ ...
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...
- Binary Tree Paths leetcode
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 环形链表【手绘漫画】面试必考之双指针(LeetCode 141)
文章目录 图解算法与数据结构 1.前言 2.实例 3.正文 4.代码 图解算法与数据结构 1.前言 今天开始的是双指针! 下面一起来看看吧!!! 让我们从一个经典问题开始: 环形链表进阶版[手绘漫画] ...
最新文章
- flask 快速入门链接
- 链路聚合-CISCO
- python 抓取微博评论破亿_一篇文章教会你使用Python定时抓取微博评论
- Java SSM1——Maven
- android 换到iphone,从安卓换到苹果到底是什么感受?最后一个让我彻底放弃了iPhone!...
- win10系统上Python和pycharm的安装及配置
- Springcloud 高效率本地加Redis双级缓存
- golang利用反射写入excel的简单工具类
- Native Instruments Maschine 2 Factory Library Mac(预置音色库)
- photoshop cc 2019 下载教程
- python tkinter画布_Python tkinter Canvas画布完全攻略(超级详细)
- 10大程序员必逛网站,良心推荐,建议收藏!
- 描述一下普适计算时代中的计算机,浅析普适计算及其应用.doc
- ADS1278学习总结
- 腾讯云服务器竞价实例是什么意思?
- 苹果系统忘记登录密码
- 解决“ImportError: cannot import name ‘_validate_lengths‘”问题
- SECS/GEM EAP 设备自动化功能原理和设计方案
- 什么人不在生死簿_高人亲眼所见的“地狱、生死簿、三世因果”(转)阴间一直是世...
- Fe3O4@SiO2硅包铁核壳纳米颗粒|链霉亲和素修饰的四氧化三铁颗粒|正硅酸乙酯(TEOS)进修饰改性磁性Fe3O4纳米颗粒
热门文章
- Javascript-history.go()和history.back()的用法和区别
- Ubuntu安装与配置
- 计算机软硬件日常维护管理规程
- CF1399E1 Weights Division (easy version)
- [Tarjan四连]Tarjan缩点
- git学习笔记-(12-git后悔药-1)
- 磁盘分区和目录的区别是什么
- Java中Collection接口
- bootstrape常用标签_bootstrap 常用data
- npm 更新_npm 的安全困扰:仅有 9.27% 的 npm 开发者使用 2FA