257. Binary Tree Paths
2016-05-15 12:50
211 查看
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
[“1->2->5”, “1->3”]
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
[“1->2->5”, “1->3”]
/** * 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> result; if(root==NULL) return result; string path; getPath(result,path,root); return result; } void getPath(vector<string>& vec,string& path,TreeNode* node) { path+=to_string(node->val); if(node->left==NULL&&node->right==NULL) vec.push_back(path); if(node->left!=NULL) { string path1=path+"->"; getPath(vec,path1,node->left); } if(node->right!=NULL) { string path2=path+"->"; getPath(vec,path2,node->right); } } };
相关文章推荐
- Linux下php7.0.6+nginx环境编译安装
- 年薪10万和年薪100万的人,究竟差在哪里?
- Qt TCP通信,多线程服务器端
- KVO、KVC、单例的使用(16.5.15)
- android开发笔记之多媒体—振动器(Vibrator)
- Apache日志分割脚本
- 第10.11周项目?-车类
- DBUtils
- Sublime Text 3 python和Package Control配置方法
- Some Chinese Android Developers Information
- spring基础知识汇总及常见面试题目总结
- 如何在PowerDesigner将PDM导出生成WORD文档或者html文件
- 列车调度(Train)
- [mac]
- 文章标题
- (版本定制)第6课:Spark Streaming源码解读之Job动态生成和深度思考
- 实现树莓派控制电机的运转
- Smalidea无源码调试 android 应用
- PowerDesigner反向工程PostgreSQL数据库
- jquery动画切换引擎插件 Velocity.js 学习01