15算法课程 257. Binary Tree Paths
来源:互联网 发布:djvu转pdf软件 编辑:程序博客网 时间:2024/05/22 00:16
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"]
solution:
写两个binaryTreePaths函数,第一个使用递归方法构造题目要求的字符串,这个函数需要合理选择参数;第二个函数用来返回结果
code:
/** * 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
- 15算法课程 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
- 设备树中设备的禁用和启用
- 数据结构与算法学习总结-数据结构基础
- ubunti16.04安装GPU tensor flow预见的问题
- 8086段寄存器偏移
- HDOJ1323Perfection(完数)
- 15算法课程 257. Binary Tree Paths
- tensorflow 学习 demo(-)
- Android 序列化和反序列化
- 互联网企业服务器资源的分配与利用
- 经典算法之基数排序两种实现
- Java equals方法特性
- mongodb centos7.3 初探
- 15算法课程 258. Add Digits
- HDOJ1326 Box of Bricks