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

本题有两种解法:递归解法和非递归解法,递归解法请参考: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. 炫酷的jquery瀑布流

    最近做了一个瀑布流效果,思路很简单 首先计算屏幕一行可以放多少个图片,然后在第二行开始,计算每一列的高度并取出最小值,将新图片加载在最小列高度下,如此循环,并且设定一个条件,当滑动到一定距离后,开始重 ...

  2. c++实现des算法

    程序分三部分,des头文件,des类实现,main函数调用. //panda //2013-4-13 //des //des.h class DES { private: //public: //明文 ...

  3. 20161007 NOIP 模拟赛 T1 解题报告

    排序 3.1 题意描述 众所周知,熟练掌握至少一种排序算法是参加NOIP的必备技能.常见的排序算法有冒泡 排序.归并排序.快速排序.奇偶排序.猴子排序.梳排序.鸡尾酒排序.臭皮匠排序等. 在这里,介绍 ...

  4. 【BZOJ】1006: [HNOI2008]神奇的国度

    http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意:在一个弦图中找最少染色数.(n<=10000, m<=1000000) #i ...

  5. HTML5_嵌套移动APP端的H5页面meta标签

    <meta charset="utf-8"> <meta content="width=device-width, initial-scale=1.0, ...

  6. 8个主要的Velocity语法使用说明

    8个主要的Velocity语法使用说明,分别是:Velocity表达式,Velocity注释,Velocity循环,Velocity条件判断,Velocity赋值,Velocity调试,Velocit ...

  7. 前端自动化工具 -- grunt 使用简介

    grunt作为一个前端构建工具,有资源压缩,代码检查,文件合并等功能. 下面就简单了解grunt的使用. 一.环境配置 grunt是基于nodejs的,所以需要一个 nodejs 环境,未了解的可以  ...

  8. js中return,this,arguments,currentStyle和getComputedStyle小析

    一.return返回值:1.函数名+括号:fn()==>return 后面的值2.所有函数默认返回值:未定义3.return后面的任何代码都不会执行二.this:当前对象1.当某个对象后边加事件 ...

  9. ZK 代码自动提示

    1.设置xsd 打开eclipse,Window-Preference,进行如下设置: 2.创建zul文件 (1)打开File—New—Other窗口,新建XML File文件: (2)选择新建文件所 ...

  10. 表单的enctype property

    enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencoded".就是说,在 ...