找到所有根到叶子的路径

深度优先搜索(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. SQLServer语句 汇总

    SQL Server语句 序号 功能 语句 1 创建数据库(创建之前判断该数据库是否存在) if exists (select * from sysdatabases where name='data ...

  2. Elasticsearch refresh vs. flush【转载】

    源地址:    http://www.jianshu.com/p/0e9f6346f1fe 问: 若一个新的文档索引进ES索引,则它在索引操作执行后约1s可以搜索到.然而我们可以直接调用_flush或 ...

  3. HttpModule的一些初步认识

    新建一个类 ValidaterHttpModuleEvents继承管道接口 IHttpModule,代码如下 public class ValidaterHttpModuleEvents:IHttpM ...

  4. MVC学习(四)几种分页的实现(2)

    在第一种分页方式中,仅仅实现了分页,但并未有体现出MVC的优势,没有体现出泛型编程思想,尤其在数据量很大的时候,分页十分缓慢,除此之外,还没有实现很好的封装,不是一个通用方法. 因此,我希望只要传入数 ...

  5. JDBC连接数据库(SQLServer和MySQL)配置总结

    [JDBC连接SQL Server] [准备工作] 1.下载Eclipse-JavaEE: 2.下载Tomcat8.0: 3.下载Microsoft JDBCDriver 4.1 for SQL Se ...

  6. iOS.OpenSource.AllInOne

    Open Source Project for iOS 所有和iOS相关的Open Source Project的汇总. 功能点 开源项目   iOS Gallery RMGallery https: ...

  7. 将html页改成jsp的两种方式

    将html页改成jsp的两种方式 作者: 字体:[增加 减小] 类型:转载 时间:2013-08-13 将html页改成jsp有两种方法,第一种是直接修改html文件,另一种是新建jsp文件.下面为大 ...

  8. C# 特殊处理使用方法

    1.时间处理 Model.PiDaiTime.ToString("yyyyMMdd") == "00010101" ? DateTime.Now.ToStrin ...

  9. MySQL关键性能监控(QPS/TPS)

    原文链接:http://www.cnblogs.com/chenty/p/5191777.html 工作中尝尝会遇到各种数据库性能调优,除了查看某条SQL执行时间长短外,还需要对系统的整体处理能力有更 ...

  10. python语法快速入门(1)

    http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...