Binary Tree Paths
来源:互联网 发布:网站源代码怎么修改seo 编辑:程序博客网 时间:2024/06/05 16:38
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> binaryTreePaths(TreeNode* root) { vector<string> result; if(root) paths(root, result, to_string(root->val)); return result; } void paths(TreeNode* node, vector<string> &result, string str) { if(!node->left && !node->right) { result.push_back(str); return; } if(node->left) { paths(node->left, result, str + "->" + to_string(node->left->val)); } if(node->right) { paths(node->right, result, str + "->" + to_string(node->right->val)); } }};
0 0
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- Binary Tree Paths
- [leetcode] Binary Tree Paths
- 257 Binary Tree Paths
- Binary Tree Paths
- [LeetCode257] Binary Tree Paths
- Binary Tree Paths
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- LeetCode:Binary Tree Paths
- leetcode Binary Tree Paths
- leetcode Binary Tree Paths
- LeetCode:Binary Tree Paths
- Binary Tree Paths
- eightQueen1.0
- 数据库设计(4)-高级ER模型构建
- 第十章 方差分析
- UIday0603:UIImageView的属性和用法 Tom猫举例
- 免费自学Cocos2d-x3.0final2014原创视频教程(56集)(适用于Cocos2d-x3.1 Cocos2d-x3.2版本全)
- Binary Tree Paths
- SQL Server2012 元数据管理器中存在错误
- [笔记] 大家一起来测试,benchmark起来(MySQL下的TPC-C,TPC-H,TPC-W)
- HDOJ 4456 Crowd 离散化+二维树状数组
- Hibernate复合主键映射
- POJ3260--The Fewest Coins(混合背包)
- 控件
- 关于Android4.0之后主线程无法访问网络的解决方案
- 一个聊天界面