leetcode-Binary Tree Paths
2015-11-06 22:46
288 查看
Difficulty:
Easy
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:
1 / \ 2 3 \ 5
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 { vector<string> res; void binaryTreePaths(TreeNode *root,string path){ if(!root->left&&!root->right) res.push_back(path+to_string(root->val)); if(root->left) binaryTreePaths(root->left,path+ to_string(root->val)+"->"); if(root->right) binaryTreePaths(root->right,path+to_string(root->val)+"->"); } public: vector<string> binaryTreePaths(TreeNode* root) { if(!root) return res; string path; binaryTreePaths(root,path); return res; } };
相关文章推荐
- cocos2d-x设计模式发掘之八:中介者模式
- JSP九大内置对象详解
- HDU - 2888 Check Corners(二维RMQ)
- probit模型
- SQL基础学习3
- Cocosd-x设计模式之七:防御式编程模式
- 杭电1418--欧拉定理
- Android_登录之服务器逻辑
- 编写高效的C程序与C代码优化 via jobbole
- Books from Joe's blog
- gevent For the Working Python Developer
- MFC 中 Radio Button 使用方法
- Libev源码分析07:Linux下的eventfd简介
- LightOJ - 1081 Square Queries(二维RMQ)
- 接口与继承-动手动脑
- 网络协议层学习笔记
- B/S架构与C/S架构的区别
- 图像处理大型科普---上下采样
- 例题4-1 UVa1339 Ancient Cipher
- 2015-11-6