Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径
深度优先搜索(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的更多相关文章
- [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. Note: A leaf is a node with no children. Example ...
- 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. Note: A leaf is a node with no children. Example ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 257 Binary Tree Paths 二叉树的所有路径
给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树: 1 / \2 3 \ 5所有根到叶路径是:["1->2->5", " ...
- (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 ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
随机推荐
- centos搭建NFS网络文件系统
NFS服务器端 安装NFS服务器非常之简单: yum install nfs-utils protmap 这样就安装好了,其中nfs-utils是提供NFS服务器程序和相应的管理工具.protmap是 ...
- python 基础理解...
class obj(object): def __getattribute__(self, *args, **kwargs): # 访问属性就会被调用 print("__getattribu ...
- protoc-gen-php --php_out: protoc-gen-php: Plugin output is unparseable.
背景 业务需要用protobuffer 进行通讯. client: php server: c++ 在github 上找到 Protobuf-PHP (https://github.com/drslu ...
- js闭包和回调
1.闭包 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.闭包有三个特性: 1.函数嵌套函数; 2.函数内部可以引用外部的参数和变量; 3.参数 ...
- SQL基础分类
我们可以把学习过的sql语言,进行分类: 1. DDL : 数据定义语言 a) 操作库表结构的语言. Create drop alter 2. DML : 数据操作语言 a) 操作数据的语言: upd ...
- Comet:基于 HTTP 长连接的“服务器推”技术
“服务器推”技术的应用 请访问 Ajax 技术资源中心,这是有关 Ajax 编程模型信息的一站式中心,包括很多文档.教程.论坛.blog.wiki 和新闻.任何 Ajax 的新信息都能在这里找到. c ...
- 第二章——建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令。比较项目的新旧版本的差别-----答题者:徐潇瑞
1.首先下载安装git,很简单所以就不详细说了,当弹出一个类似的命令窗口的东西,就说明Git安装成功 2.因为Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识 3.接着,注册githu ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- C语言中scanf()的用法!
好文章转自:http://blog.tianya.cn/blogger/post_show.asp?BlogID=287129&PostID=3668453 scanf详解 scanf 原型: ...
- Block 传值
A 界面: - (IBAction)gotoVC:(id)sender { //必须在事件发生时调用Block,每次Block对应一次初始化 cvc = [[CViewController alloc ...