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 ...
随机推荐
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- Codeforces Beta Round #1
A题,水题. B题也是水题,弄的比较麻烦,前几天队内赛见过,水题怎么水都能过. C题 题意:给出正n边形上的三个点,求最小的正n边形的面积. 以前貌似见过此题.思路也没什么进展,就是枚举n,通过旋转a ...
- Android -- 编辑框更改样式
1. 效果图
- Js C# 实现跨域访问数据
使用项目一的js调用项目二的数据 1.项目一 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta ...
- Android studio 一个项目中添加两个module遇到的bug
1.在一个Android studio中,我添加了一个模块,然后就是各种bug 找到到R 是在module 名上面 右键 Make Module '模块名' 经过各种google 的时候发现了 htt ...
- .NET易忘备留 ORACLE存储过程调用
1.Oracle存储过程调用[返回信息,单体或者列表] public IResult FundBuild(string partnerId,string userId, DateTime beginD ...
- light oj 1047 - Neighbor House 动态规划
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/C 题目: Description The peopl ...
- 序列化,反序列化,模拟ATM机
package com.bank.unionpay; //银行卡的接口 public interface I_yinhangka { //抽象方法 //public abstract默认修饰抽象的 p ...
- [转] - bashrc与profile的区别
bashrc与profile的区别 要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是login shell 和non-login shell. ...
- 完美扫描PHP特殊一句话后门
<?php /********************** 作者 Spider 网上公布的各种PHP后门全军覆没 针对一些特殊变形的后门需要自己添加特征 误报率不到百分之一 ********** ...