找到所有根到叶子的路径

深度优先搜索(DFS), 即二叉树的先序遍历。

 /**
* 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> vs_;
void dfs(TreeNode* root, string s){
if(!root) return;
if(!root->left && !root->right){
char t[] = "";
sprintf(t, "->%d", root->val);
vs_.push_back(s + string(t));
return;
}
else{
char t[] = "";
sprintf(t, "->%d", root->val);
dfs(root->left, s + string(t));
dfs(root->right, s + string(t));
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vs_.clear();
if(!root) return vs_;
char t[] = "";
sprintf(t, "%d", root->val);
string s(t);
if(!root->left && !root->right){
vs_.push_back(s);
return vs_;
}
dfs(root->left, s);
dfs(root->right, s);
return vs_;
}
};

Leetcode 257 Binary Tree Paths 二叉树 DFS的更多相关文章

  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. Note: A leaf is a node with no children. Example ...

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

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

  4. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  5. Leetcode 257. Binary Tree Paths

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

  6. 257 Binary Tree Paths 二叉树的所有路径

    给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树:   1 /   \2     3 \  5所有根到叶路径是:["1->2->5", " ...

  7. (easy)LeetCode 257.Binary Tree Paths

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

  8. Java [Leetcode 257]Binary Tree Paths

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

  9. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

随机推荐

  1. PHP魔法方法的使用

    1.__get / __set 当类没有要存取的属性时,就调用这两个函数 $obj = new IMooc\Object();$obj->title = "hello";ec ...

  2. python学习之——小闹钟(持续完善ing)

    "啊,坏了,我忘了那啥啥了~~~" 为了不坏了,动手做一个小闹钟吧,一点点完善的过程一定美好极了,必像等待培育许久的花儿绽放一样,不多说,加油,期待↖(^ω^)↗ #! /usr/ ...

  3. USB协议(1)

    今天开始学习USB协议,精挑细选,我决定使用<圈圈教你玩USB>这本书,并且参考网友翻译的<USB2.0中文协议>. 这两本书都可以在ishare.sina.com.cn 即新 ...

  4. mybatis 控制台打印sql

    开发时调试使用 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBe ...

  5. 关于VMWARE 上对于不小心VMWare Tools Easy Install 后卡死的解决方法

    PS 要想修改首先开机的时候会进入命令行,输入用户名,密码后,可以用startx来进入图形界面(也有人将/etc/init.d/ligtmd start可以懂事本人那次是不行的) 通常网上人们的解决办 ...

  6. Struts2中请求参数的接收方式和ModelDriven机制及其运用

    odelDriven 为什么需要ModelDriven 所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar ...

  7. 何时使用Swift Structs和Classes

    Swift 圈中有一个被反复讨论的话题是:何时使用struct,何时使用class.我觉得今天我也要给出我的个人观点. 值 VS 引用 答案真的很简单了:当你需要用值语义的时候使用class,需要用引 ...

  8. strace 监控所有php-fpm worker

    strace命令详解  http://linux.die.net/man/1/strace strace -tt -T $(pidof 'php-fpm: pool www' | sed 's/\([ ...

  9. Redis在CentOS6.4中的安装

    首先,介绍一下Redis数据库.Redis是一种面向“键/值”对数据类型的内存数据库,可以满足我们对海量数据的读写需求. 1)redis的键只能是字符串: 2)redis的值支持多种数据类型: a:字 ...

  10. eclipse中无法使用fat.jar

    因为某种需要,我要打jar包,而eclipse中自带的打包功能又太过于繁琐,因此找到这个插件.不过尝试了许久都没有成功,最后终于找到了原因,是因为该插件的版本太低的缘故.相见:https://code ...