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 ...
随机推荐
- python之优雅处理套接字错误
#!/usr/local/bin/python3.5 #coding:utf-8 import sys import socket import argparse def main(): #setup ...
- python-flask 框架使用 flask_mongoengine
开发环境配置 再使用 mongodb 之前,需要先安装 pymongo ,以及flask_mongoengine 1. 切换到 virtualenv 环境 . /pyenv/bin/activate ...
- Robolectric 配置
费了些工夫,已配好,按记录留记录 按官网操作http://robolectric.org/getting-started/ 1引包 testCompile "org.robolectric: ...
- const限定符
1 const的作用 便于进行类型检查.可以保护被修饰的东西.避免不必要的内存分配.为函数重载提供一个参考. 2 const成员函数 const成员函数只能访问数据成员的值,而不能修改他. #incl ...
- canvas 画六边形
<section class="m1-c"> <div class="m1-t clearfix"> <ul> <li ...
- 高并发访问mysql时的问题(一):库存超减
如果在对某行记录的更新时不采取任何防范措施,在多线程访问时,就容易出现库存为负数的错误. 以下用php.mysql,apache ab工具举例说明: mysql表结构 CREATE TABLE `yx ...
- apache的安装,启动和停止
一.apache服务器的安装 安装步骤直接傻瓜式进行安装.并没有太大的难点.apache的配置是学习的重点和难点. 安装好后再浏览器地址栏输入http://localhost.若能够成功安装,则会显示 ...
- 《HTML5秘籍》学习总结--2016年7月24日
前段时间因为工作中看到同事使用了一个type为date的<input>元素,直接就形成了一个日期选择的表单控件,当时觉得很神奇,以为是什么插件,就问了同事是怎么做出来的,同事告诉我这是HT ...
- NSCalenda日历类
1. //将数据库时间和当前时间相比,得出时间差. + (NSString *)dateDescriptionWithDate:(NSDate *)date{ // NSCalendar日历类,提供了 ...
- sublim text3 配置
喜欢用sublime,但每次换环境都要重新百度下配置,太麻烦,故在此把自己喜欢的配置记录下来 sublime text2 可以直接在设置里改,但sublime text3不能直接在设置中改值,只能在s ...