<LeetCode OJ> 257. Binary Tree Paths
2016-01-10 21:43
381 查看
257. Binary Tree Paths
My SubmissionsQuestion
Total Accepted: 29282 Total
Submissions: 113527 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"]
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first
Search
Hide Similar Problems
(M) Path Sum II
/** * 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 strpath) { if(!node->left && !node->right){//叶子 result.push_back(strpath); return ; } if(node->left) getDfsPaths(result, node->left, strpath+"->"+to_string(node->left->val)); if(node->right) getDfsPaths(result, node->right, strpath+"->"+to_string(node->right->val)); } vector<string> binaryTreePaths(TreeNode* root) { vector<string> ret; if(!root) return ret; getDfsPaths(ret, root, to_string(root->val)); return ret; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50493936
原作者博客:http://blog.csdn.net/ebowtang
相关文章推荐
- VBScript.RegExp 正则表达式excel vba 学习经验
- 【UVa】[227]Puzzle
- C#中HttpWebRequest用Post提交Content-Type: multipart/form-data;
- OpenGL系统设计——求帧速度
- 创建带子菜单的选项菜单
- Mybatis深入之事务管理
- 非线性思维 -- 大前研一
- 等待队列 赏析
- Go 四篇
- [leetcode oj 191]Number of 1 Bits
- HPU:1232畅通工程(并查集)
- SSH框架
- 基础编程模型(译自 Algorithms Fourth Edition)
- 5.3.5 namedtuple() 创建命名字段的元组结构
- 292. Nim Game
- 未来十年,Blockchain 会如何重塑互联网世界?
- 简易在线投票系统(php)——发布投票页面
- 5.3.5 namedtuple() 创建命名字段的元组结构
- Lingo
- cocos2d-x一个函数实现精灵变灰效果