[257]Binary Tree Paths
来源:互联网 发布:今日特惠淘宝优站 编辑:程序博客网 时间:2024/05/16 05:21
【题目描述】
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> vec; string str=""; if(root==NULL) return {}; path(root,vec,str); return vec; } void path(TreeNode* root,vector<string> &vec,string str){ if(root==NULL) return; if(str=="") str=str+to_string(root->val); else str=str+"->"+to_string(root->val); if(root->left==NULL&&root->right==NULL){ vec.push_back(str); return; } if(root->left!=NULL){ path(root->left,vec,str); } if(root->right!=NULL){ path(root->right,vec,str); } }};
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
- 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
- 基于HTML5实现3D监控应用流动效果
- VS2010配置SDL2.0步骤
- Spreading the Wealth(Uva 11300)单变量极值问题
- UVA185map的使用
- 【黑马程序员】【C语言】注释
- [257]Binary Tree Paths
- 只有程序员看的懂面试圣经
- 【黑马程序员】【C语言】变量
- 视频切割的精确定位问题
- 直接插入排序算法
- 股票的强弱分析 RSI
- Android音乐编程:管理音频焦点
- 【黑马程序员】【C语言】选择结构-if语句
- C语言第一天