257. Binary Tree Paths
来源:互联网 发布:linux socket多线程 编辑:程序博客网 时间:2024/06/03 07:15
思路:写两个binaryTreePaths函数,第一个使用递归方法构造题目要求的字符串,这个函数需要合理选择参数;第二个函数用来返回结果:
/** * 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 binaryTreePaths(TreeNode* root, string s, vector<string> &result) { if (!root->left && !root->right) { result.push_back(s); return; } if (root->left) binaryTreePaths(root->left, s + "->" + to_string(root->left->val), result); if (root->right) binaryTreePaths(root->right, s + "->" + to_string(root->right->val), result); } vector<string> binaryTreePaths(TreeNode* root) { vector<string> vec; if (root == NULL) return vec; binaryTreePaths(root, to_string(root->val), vec); return vec; }};
阅读全文
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
- idea常用快捷键
- SpannableString文字操作
- Leetcode(W1):169.Majority Element
- 【LeetCode】4.Median of Two Sorted Arrays
- 根据一个点获取窗口句柄的API
- 257. Binary Tree Paths
- 数据库连接Java
- matlab2c使用c++实现matlab函数系列教程-normpdf函数
- 【BZOJ1426】收集邮票 期望DP
- js正则中\b的用法
- 本无完美
- Android--(6)--详解ImageButton属性
- 2017-09-10 LeetCode_017 Letter Combinations of a Phone Number
- 算法分析与设计丨第一周丨LeetCode(2)——Different Ways to Add Parentheses(Medium)