Leetcode题目:Binary Tree Paths
题目:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1/ \ 2 3\5
All root-to-leaf paths are:
["1->2->5", "1->3"]
题目解答:使用递归的方式来处理这道题目,每到叶子节点,就进行一次输出。
代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
if(root == NULL)
return res;
stringstream ss;
ss << root -> val;
if((root -> left == NULL) && (root -> right == NULL))
{
res.push_back(ss.str());
return res;
}
else
{
getPaths(root,ss.str());
}
return res;
}
void getPaths(TreeNode *root,string s)
{
if((root -> left == NULL) && (root -> right ==NULL))
{
res.push_back(s);
}
if(root -> left != NULL)
{
stringstream ss;
ss << s << "->" << root -> left -> val;
getPaths(root -> left , ss.str());
}
if(root -> right != NULL)
{
stringstream ss;
ss << s << "->" << root -> right -> val;
getPaths(root -> right , ss.str());
}
}
private:
vector<string> res;
};
转载于:https://www.cnblogs.com/CodingGirl121/p/5440096.html
Leetcode题目:Binary Tree Paths相关推荐
- 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 -- Binary Tree Paths
http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...
- LeetCode 257 Binary Tree Paths
题目描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tre ...
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- Binary Tree Paths leetcode
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- LeetCode: 107. Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode: 103. Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- LeetCode: Flatten Binary Tree to Linked List
LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...
- CF750G New Year and Binary Tree Paths(数位dp二进制+数学)
CF750G New Year and Binary Tree Paths description solution code description 题目链接 一颗无穷个节点的完全二叉树. 求有多少 ...
最新文章
- python要点简要-一日精通python
- mac安装mysql修改密码_Mac下安装MySQL 5.7.28并且修改root密码-Go语言中文社区
- 包python_Python 包:
- npm介绍以及常用命令
- 2014年 第5届 蓝桥杯 Java B组 省赛解析及总结
- Myeclipse使用DB Browser连接数据库错误:OPTION SQL_SELECT_LIMIT=DEFAULT
- 请解释Spring Bean 的生命周期?
- LuoguP5504 [JSOI2011]柠檬
- wangEditor-3.1.1 自己扩展的,增加代码块行号
- nemesis什么车_马力2100匹《Trion Nemesis》谜样超跑诞生中?
- rust和gta5哪个吃配置_晨报:Steam周销量榜 曝次世代GTA5用大镖客2引擎
- 移动办公之路的行业探索
- MySQL 索引最左匹配原则的理解
- NI视觉视觉软件简介
- ROC(AUC)的显著性检验
- 浏览器设置代理服务器插件SwitchyOmega配置
- 简单枚举(ZJM要抵御宇宙射线)
- excel这几大数据处理技巧,高效率操作技能,今天免费交给你!
- Android---RecyclerView网络请求图片加载
- Unity - URP RenderFeature - 实现类似多 Pass 的 XRay: Rim、Pattern