<LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths
Submissions: 113527 Difficulty: Easy
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:
void getDfsPaths(vector<string>& result, TreeNode* node, string strpath) {
if(!node->left && !node->right){//叶子
result.push_back(strpath);
return ;
}
if(node->left)
getDfsPaths(result, node->left, strpath+"->"+to_string(node->left->val));
if(node->right)
getDfsPaths(result, node->right, strpath+"->"+to_string(node->right->val));
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> ret;
if(!root)
return ret; getDfsPaths(ret, root, to_string(root->val));
return ret;
}
};
小结:
1。深度搜索应该立马条件反射,採用前序式遍历(假设用递归的话)
2,深度优先搜索应该立马联想到栈来实现迭代
3,递归具有保存变量信息的功能,有时候值得利用
联动第二十二题
【1】 22. Generate Parentheses。http://blog.csdn.net/ebowtang/article/details/50557414
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50493936
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 257. Binary Tree Paths的更多相关文章
- [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 ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 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 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 解题报告(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 ...
随机推荐
- PAT1017
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- Could not connect to Redis at 127.0.0.1:6379: Connection refused
启动redis: redis-server ../redis.conf redis启动成功后 执行命令行redis-cli报:Could not connect to Redis at 127.0. ...
- 手机安装app总是显示未安装
手机安装软件总是显示未安装 查看是否开启了护眼模式或者护眼工具等干扰屏幕的软件.关掉,再安装即可
- (转)iOS-蓝牙学习资源博文收集
ios蓝牙开发(一)蓝牙相关基础知识 ios蓝牙开发(二)蓝牙中心模式的ios代码实现 ios蓝牙开发(三)app作为外设被连接的实现 ios蓝牙开发(四)BabyBluetooth蓝牙库介绍 暂未完 ...
- [python]做一个简单爬虫
为什么选择python,它强大的库可以让你专注在爬虫这一件事上而不是更底层的更繁杂的事 爬虫说简单很简单,说麻烦也很麻烦,完全取决于你的需求是什么以及你爬的网站所决定的,遇到的第一个简单的例子是pas ...
- Chrome 浏览器访问 Google 学术出现问题 “but your computer or network may be sending automated queries. ”
问题: Chrome 浏览器访问 Google 学术出现如下的问题 : ... but your computer or network may be sending automated querie ...
- git repo gerrit 的关系
Git作为一个版本控制工具,功能很强大,新建分支,切换分支都很快,小团队用Git就能很好地管理好了,但如果是Android系统如此庞大的工程呢,我们知道全套Android源码是很大很大的,目录结构也很 ...
- cf 542E - Playing on Graph
cf 542E - Playing on Graph 题目大意 给定一个\(n\le 1000\)个点的图 求经过一系列收缩操作后能否得到一条链,以及能得到的最长链是多长 收缩操作: 选择两个不直接相 ...
- 自动合并多个文件如js css等 可以增加效率
原文发布时间为:2011-01-13 -- 来源于本人的百度文章 [由搬家工具导入] 原文地址:http://www.codeproject.com/KB/aspnet/HttpCombine.asp ...
- 教你怎么使用Windows7系统自带的备份与还原的方法
原文发布时间为:2010-09-09 -- 来源于本人的百度文章 [由搬家工具导入] 继续单击“下一步”按钮,在其后界面中检查上述备份设置是否正确,如果不正确的话可以直接单击“取消”按钮,重新设置备份 ...