【LeetCode】257. Binary Tree Paths
2016-03-30 20:45
330 查看
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
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 { public: void getDfsPaths(vector<string>& result, TreeNode* node, string sln) { if(!node->left && !node->right) { result.push_back(sln); return; } if(node->left) getDfsPaths(result,node->left, sln+"->"+to_string(node->left->val)); if(node->right) getDfsPaths(result,node->right, sln+"->"+to_string(node->right->val)); } vector<string> binaryTreePaths(TreeNode* root) { vector<string>result; if(root==NULL) return result; getDfsPaths(result,root, to_string(root->val)); return result; } };
相关文章推荐
- 2015-2016 下半学期 第三周 训练
- JNI/NDK开发指南(开山篇)
- 【Java学习-J.160330.0.3】笔记2-Linux基础
- Delphi数据类型转换(有几个字符串函数没见过,比如StringToWideChar和WideCharToString)
- having筛选结果集
- 加快XCode的编译链接速度
- 第3周项目4-用穷举法解决组合问题(3)年龄几何
- Sverlet生命周期和工作机制
- UVa401 - Palindromes
- bash shell 编程练习二
- 面试题69:序列化二叉树
- Mac下配置mysql环境变量的另一种方式
- There was a problem getting an AppDomain to run the transformation from the host. The process cannot
- VK Cup 2016 - Round 1 (Div. 2 Edition)C. Bear and Forgotten Tree 3(构造)
- Java中权限修饰问题private -> protected -> (default) -> public
- docker简明教程(一)
- 单向链表排序
- eclipse启动tomcat成功,但却一直处在starting状态
- 职场加薪步步高升的五大法则
- IT行业导览-4.2-产品设计师