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

本题有两种解法:递归解法和非递归解法,递归解法请参考: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. SolrCloud 5.x 集群部署方法

    CentOS下安装Solr5.3    http://www.centoscn.com/image-text/install/2015/0918/6190.html solr5.3.1 集群服务搭建 ...

  2. CentOS评估磁盘I/O性能读写极限测试

    用一个fio工具 安装 yum -y install fio 二,FIO用法: 随机读:fio  -direct=1 -iodepth 1 -thread -rw=randread -ioengine ...

  3. Redis在windows下的安装使用

    下载的windows版本是redis-2.0.2,解压到D盘下: D:\redis-2.0.2 启动Redis服务(conf文件指定配置文件,若不指定则默认): D:\redis-2.0.2>r ...

  4. iOS性能优化:Instruments使用实战

    iOS性能优化:Instruments使用实战   最近采用Instruments 来分析整个应用程序的性能.发现很多有意思的点,以及性能优化和一些分析性能消耗的技巧,小结如下. Instrument ...

  5. tab左右箭头切换(修改后)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JAVA6开发WebService (四)——SAAJ调用WebService

    转载自http://wuhongyu.iteye.com/blog/810571 前面写了个JAX-WS的小例子,看到用JAVA6开发WebService确实很简单,也很方便,不过前面也说了,JAVA ...

  7. spring security LDAP获取用户信息

    很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...

  8. 【HDU 4747 Mex】线段数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:有一组序列a[i](1<=i<=N), 让你求所有的mex(l,r), mex ...

  9. 搭建vpn

    之前买的vpn,对linux支持很不友好,家里装的又是ubuntu.突然一想自己买个vps搭个vpn. 先买了host1plus的vps,一个月30块,配了两天,pptp,l2tp,shadow so ...

  10. Eclipse 官方简体中文语言包下载地址及安装方法

    Eclipse 官方简体中文语言包下载地址及安装方法 打开Eclipse Babel Project 主页: http://www.eclipse.org/babel/downloads.php 根据 ...