257 Binary Tree Paths
2015-09-25 20:13
260 查看
题意:给定二叉树,找出所有根到叶子的路径
分析: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> binaryTreePaths(TreeNode* root) { vector<string> ans; //if(root==nullptr) return ans; string curPath=""; dfs(ans,curPath,root); return ans; } string getstring ( const int n ){ std::stringstream newstr; newstr<<n; return newstr.str(); } void dfs(vector<string> &paths,string curPath,TreeNode* root){ if(root==nullptr) return; if(root->left==nullptr&&root->right==nullptr){ paths.push_back(curPath+getstring(root->val)); return; } dfs(paths,curPath+getstring(root->val)+"->",root->left); dfs(paths,curPath+getstring(root->val)+"->",root->right); } };
相关文章推荐
- 一个json字符串
- hdu1213
- Objective-C 基础知识之 (十六):内存管理原则一
- 如何处理信息是目前的首要矛盾——判断力
- MongoDB索引与性能
- 线性表
- 大家好我又滚回来了
- Java核心技术第3章(2)
- Bex64
- hdu1213
- Fragment 生命周期 全局变量的声明位置
- Android 图片缓冲的管理-内存缓存
- quick-find【连通图】
- 【转载】GridView自动排序
- 《深入理解Linux网络技术内幕》阅读笔记(三)
- 蓝懿iOS培训日志20 MKMap
- GridView
- HTTP Live Streaming协议草案各版本加入的新特性
- 4.2二叉排序树
- Codeforces Beta Round #3——B. Lorry