查找二叉树中根节点到叶子节点的所有路径:

本题有两种解法:递归解法和非递归解法,递归解法请参考: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的更多相关文章

  1. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 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 ...

  3. Java实现 LeetCode 257 二叉树的所有路径

    257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...

  4. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  6. (easy)LeetCode 257.Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  8. LeetCode 257 二叉树的所有路径

    题目: 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5&quo ...

  9. [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 ...

随机推荐

  1. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  2. 【ZOJ】3329 One Person Game

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个色子,分别有k1.k2.k3个面,权值分别是1-k1, 1~ ...

  3. [BZOJ2795][Poi2012]A Horrible Poem

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 261  Solved: 150[Subm ...

  4. DockerFile 参数详解

    Docker 指令: From --- ENV ---设置环境变量ENV App_DIR /appp Add 和 Copy 可以复制文件到容器里面 .区别 Add 可以写网络的链接地址 Add 支持解 ...

  5. .NET设计模式: 工厂模式

    .NET设计模式: 工厂模式(转) 转自:http://www.cnblogs.com/bit-sand/archive/2008/01/25/1053207.html   .NET设计模式(1): ...

  6. 实现Map-side Join和Reduce-side Join(转)

    在大数据处理场景中,多表Join是非常常见的一类运算.为了便于求解,通常会将多表join问题转为多个两表连接问题.两表Join的实现算法非常多,一般我们会根据两表的数据特点选取不同的join算法,其中 ...

  7. 让wego微购购物分享系统采集拍拍数据功能之腾讯paipai功能采集插件

    wego是一款很不错的导购系统,无论前后台设计风格和功能都还不错,可有时我们的确需要一些自定义的功能,毕竟万千世界,大家都做一样的东西,采集同样的数据,能不烦吗?哈哈,今天就奉献上一个wego拍拍采集 ...

  8. onselectstart

    以前在做图片滚动时,在双击左右箭头,快速切换图片滚动时,会选择附近区域的文字,感觉不是很好,今天在同事在分享时,讲到了这个问题, 试了一下,不错,解决了问题IE及Chrome下的方法一样,对相应的元素 ...

  9. express3.0安装并使用layout模板

    转自:http://cnodejs.org/topic/5073989b01d0b801480520e4 1.安装 express-partials. 方法一:运行 cmd 用 npm install ...

  10. jsp统测

    . 解析:B/S架构并不是C/S架构的替代品,有些程序例如大型的网络游戏一般使用的是C/S架构 .  解析:web-inf目录中的文件不能被客户端直接访问.所以正确答案为c 解析:jsp的是jsp内置 ...