leetcode 257
查找二叉树中根节点到叶子节点的所有路径:
本题有两种解法:递归解法和非递归解法,递归解法请参考:http://blog.csdn.net/booirror/article/details/47733175
该博主对递归算法的讲解不多,但是代码还是很容易看懂的。
刚刚又看到了一个代码写的更好、更简洁的版本,这个版本应该是我看到的所有递归解法中代码最简洁的一个版本,学习了。网址为:http://www.2cto.com/kf/201601/456116.html
非递归解法如下:
1、设置一个二维数组,基本元素是树上的节点,其一维数组表示路径
2、用根节点初始化一个一维数组(第一条路径),并将这个数组作为二维数组的第一个元素
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) { vector<string> res(0);
if(!root)
return res; vector< vector<TreeNode *> > ptr_res;
vector<TreeNode *> ptr_temp(1, root);
ptr_res.push_back(ptr_temp); while(!ptr_res.empty())
{
for(auto i = ptr_res.begin(); i != ptr_res.end(); i ++)
{
auto j = i->at(i->size() - 1);
if(! j->left && ! j->right)
{
string s;
for(auto j = i->begin(); j != i->end(); j ++)
{
char num[8];
sprintf(num, "%d", (*j)->val);
string temp(num);
if(j == i->begin())
s += temp;
else
{
string ch("->");
s += ch;
s += temp;
}
} res.push_back(s);
ptr_res.erase(i);
break;
}
else if(j->left && j->right)
{
vector<TreeNode *> path_temp(i->begin(), i->end());
i->push_back(j->left);
path_temp.push_back(j->right);
ptr_res.insert(++i, path_temp);
break;
} else
{
if(j->left)
i->push_back(j->left);
else
i->push_back(j->right);
break;
} } } return res; }
};
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] 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 二叉树的所有路径
257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...
- 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 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- (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 二叉树的所有路径
题目: 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5&quo ...
- [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
随机推荐
- 【BZOJ】1513: [POI2006]Tet-Tetris 3D
题意 给\(n(1 \le n \le 20000)\)个立方体\((x, y, z)\),依次落下.求所有立方体落下完了以后最高的高度. 分析 平面求最大值,平面更新最大值. 题解 二维线段树走起, ...
- USACO 5.5 Picture(周长并)
POJ最近做过的原题. /* ID: cuizhe LANG: C++ TASK: picture */ #include <cstdio> #include <cstring> ...
- iOS 电话在后台运行时,我的启动图片被压缩
一,经历 <1> 第一感觉是启动图片没有设置好,长度设置小了.但是和网上说的正确方式相比,没什么差别. <2> 害怕是控制器影响的,又新建了一个项目,来检验启动图片是否设置成功 ...
- vue.js中v-for的使用及索引获取
1.v-for 直接上代码. 示例一: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- 转载:C# this.invoke()作用 多线程操作UI 理解二
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); //测试的窗体 public class ...
- win7/IE8无法加载QCbin的插件
pian A: 1.控制面板->系统和安全->更改用户账户控制设置->安全等级调至最低->关机重启 2.打开IE浏览器->工具->Internet选项->高级 ...
- [LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
- android-ImageView及其子类
一.知识概要 ImageView继承自View,能显示任何Drawable对象; ImageView支持的常用XML属性及相关方法: android:adjustViewBounds 设置Ima ...
- thinkphp添加空数据的解决办法
thinkphp真是个麻烦的东西,各种小问题,其中字段映射的表单名不能与数据库的字段名称相同,否则会添加空数据! 还有自动完成的名称要与字段映射后的名称相同,否则自动完成不会起作用! 还有自动验证的字 ...
- github上怎么预览页面
直接在 http://htmlpreview.github.io/? 后面加上git上的地址就可以预览了 比如 http://htmlpreview.github.io/?https://github ...