【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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”]
(二)解题
题目大意:给定一个二叉树,输出所有根节点到叶子节点的路径。
解题思路:采用深度优先搜索,碰到叶子节点就输出该条路径。
需要注意以下几点(也是我在解题过程中犯的错误):
- 需要考虑节点值为负数的情况,要转成string
- 要按照题目给定的格式来输出。
下面看具体代码:
/**
* 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) {
vector<string> ret;
string tmp;
if(root!=NULL) dfsTreePaths(root,ret,tmp);
return ret;
}
void dfsTreePaths(TreeNode* root,vector<string>& ret, string tmp)
{
if(root->left == NULL&& root->right==NULL) {//如果为叶子节点就输出
char temp[10];
sprintf(temp, "%d", root->val);//将整数转换成string
tmp += string(temp);
ret.push_back(tmp);
return;
}
char temp[10];
sprintf(temp, "%d", root->val);//将整数转换成string
tmp += string(temp);
tmp +="->";
if(root->left !=NULL) dfsTreePaths(root->left,ret,tmp);//继续搜索左子树
if(root->right !=NULL) dfsTreePaths(root->right,ret,tmp);//继续搜索右子树
}
};
【一天一道LeetCode】#257. 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] 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 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- [leetcode]257. Binary Tree Paths二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
随机推荐
- 面试题2:实现Singleton模式
题目:设计一个类,我们只能生成该类的一个实例.
- Python中生成器和迭代器的功能介绍
生成器和迭代器的功能介绍 1. 生成器(generator) 1. 赋值生成器 1. 创建 方法:x = (variable for variable in iterable) 例如:x = (i f ...
- 如何查看cisco 生成树状态
如何查看cisco 生成树状态 Role(角色): 1.Desg(指定端口) 2.Root(根端口) 3.Altn(替换端口) Sts(状态): 1.FWD(转发) 2.BLK( ...
- js去掉最后一个字符
console.log(("0,1,2,3,4,5,".slice(0,-1)))
- vue mint-ui 实现省市区街道4级联动(仿淘宝京东收货地址4级联动)
demo及源码地址 https://github.com/artiely/citypicker 先去下载一个“省份.城市.区县.乡镇” 四级联动数据,然后 引入 import { Picker } f ...
- Docker学习系列(三)Docker搭建gitlab的两种方式
一.直接下载docker-ce 1.拉取gitlab/gitlab-ce Randy:~ Randy$ docker pull gitlab/gitlab-ce Using default tag: ...
- 解析配置文件redis.conf
units单位: # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 ...
- delphi 线程教学第五节:多个线程同时执行相同的任务
第五节:多个线程同时执行相同的任务 1.锁 设,有一个房间 X ,X为全局变量,它有两个函数 X.Lock 与 X.UnLock; 有如下代码: X.Lock; 访问资源 P; ...
- PHP 是什么
PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言. PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本 ...
- PHP $_POST 变量
$_POST 变量 预定义的 $_POST 变量用于收集来自 method="post" 的表单中的值. 从带有 POST 方法的表单发送的信息,对任何人都是不可见的(不会显示在浏 ...