leetcode-257-Binary Tree Paths
来源:互联网 发布:化合物数据库 编辑:程序博客网 时间:2024/06/05 14:10
问题
题目:[leetcode-257]
思路
深度优先即可。
代码
/** * 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) { std::vector<std::string> ret; if(!root) return ret; std::string s; dfs(root, s, ret); return ret; }private: void dfs( TreeNode* root, std::string str, std::vector<std::string>& ret ){ if(!root) return; if(!(root->left)&&!(root->right)){ str += int2Str(root->val); ret.push_back(str); } else{ str += int2Str(root->val); str += "->"; dfs(root->left, str, ret); dfs(root->right, str, ret); } str.pop_back(); } std::string int2Str(int val){ std::stringstream ss; ss << val; return ss.str(); }};
0 0
- leetcode 257: Binary Tree Paths
- leetCode #257 Binary Tree Paths
- LeetCode(257)Binary Tree Paths
- [leetcode 257]Binary Tree Paths
- leetcode[257] Binary Tree Paths
- LeetCode 257: Binary Tree Paths
- leetcode 257 : Binary Tree Paths
- 【LEETCODE】257-Binary Tree Paths
- [leetcode 257] Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- leetcode 257:Binary Tree Paths
- leetcode-257-Binary Tree Paths
- LeetCode 257Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- leetcode 257 Binary Tree Paths
- LeetCode-257:Binary Tree Paths
- 二分查找递归非递归实现并分析
- 整理牛人看文献的方法
- 威眼电脑监控屏幕(3.7.2)升级优化内容
- 解决win10缩放125%时,字体模糊的问题
- 正则表达式的简单入门
- leetcode-257-Binary Tree Paths
- python数据分析概述
- 【初学kali】“apt-get”的应用
- 查找最大元素
- Linux14.04+cuda7.5+anaconda2+opencv3.0+caffe+Matlab 艰辛之路
- 透明加密软件技术优势在哪
- 基于机器学习方法通过BBC新闻头条进行道琼斯指数DJIA的预测
- 排序--堆排序
- 简单使用Git和Github来管理自己的代码和读书笔记