257. Binary Tree Paths
来源:互联网 发布:手机淘宝无法识别图片 编辑:程序博客网 时间:2024/06/17 13:12
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 {public: vector<string> ans; void dfs(TreeNode* root, string s){ if(root == NULL) return; char buf[16]; sprintf(buf, "%d", root->val); if(root->left == NULL && root->right == NULL){ s += buf; ans.push_back(s); return; } dfs(root->left, s + buf + "->"); dfs(root->right, s + buf + "->"); } vector<string> binaryTreePaths(TreeNode* root) { string s; dfs(root, s); return ans; }};
0 0
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 算法之路二:刘汝佳算法竞赛入门经典:c++模板
- [bzoj3721]Final Bazarek
- 设备驱动中的并发控制 (2)
- Ubuntu14.04 ROS Indigo安装教程,以及卸载方法
- Codeforces Round #390 (Div. 2) C. Vladik and chat (DP/记忆化搜索)
- 257. Binary Tree Paths
- CentOS6 配置FTP服务器
- 一份 c++的DoxyGen 的注释模板
- 72. Edit Distance
- 最容易理解的对卷积(convolution)的解释
- Selenium参数化-使用eclipse Arguments
- 搜索旋转排序数组
- LeetCode 111. minimum depth of binary tree
- 1056. 组合数的和(15)