Medium: Binary Tree Paths Add to List
来源:互联网 发布:淘宝网卖精密管违法吗 编辑:程序博客网 时间:2024/06/06 03:11
257. Binary Tree Paths Add to ListDescriptionHintsSubmissionsSolutionsTotal Accepted: 102130Total Submissions: 276990Difficulty: EasyContributor: LeetCodeGiven a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]
代码如下:
class Solution {public: void binaryTreePaths(vector<string>& result, TreeNode* root, string t) { if(!root->left && !root->right) { result.push_back(t); return; } if(root->left) binaryTreePaths(result, root->left, t + "->" + to_string(root->left->val)); if(root->right) binaryTreePaths(result, root->right, t + "->" + to_string(root->right->val)); } vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; if(!root) return result; binaryTreePaths(result, root, to_string(root->val)); return result; }};
- 运用二叉树解题常用的递归思路,将复杂的问题简单化。
- 将每个结点的元素转化为一个string类放于t中,再一个一个递归的压入result,再返回。
0 0
- Medium: Binary Tree Paths Add to List
- Add to List 257. Binary Tree Paths
- Add to List 563. Binary Tree Tilt
- [Leetcode 114, Medium] Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List(medium)
- 114. Flatten Binary Tree to Linked List (Medium)
- leetcode104~Maximum Depth of Binary Tree Add to List
- 104.Maximum Depth of Binary Tree Add to List
- leetcode124~Binary Tree Maximum Path Sum Add to List
- LeetCode98. Validate Binary Search Tree Add to List
- 104. Maximum Depth of Binary Tree Add to List DescriptionHintsSubmissions
- LeetCode 98. Validate Binary Search Tree Add to List
- LeetCode 99. Recover Binary Search Tree Add to List
- Add to List 111. Minimum Depth of Binary Tree
- 【LeetCode】Add to List 606. Construct String from Binary Tree
- leetcode- Add to List 669. Trim a Binary Search Tree
- binary tree to list
- Add Sibling to Binary Tree
- H5 canvas实现客户端压缩图片上传
- 139.病毒库的更新
- 第3章 需求获取
- POJ 2386 Lake Counting
- Android框架之路——Retrofit2.0的初窥(包含Gson)
- Medium: Binary Tree Paths Add to List
- 破解jquery.dataTables.editor 1.6.2
- 曾经的笔试面试
- Anaconda更新失败
- 从输入 URL 到页面加载完成的过程中都发生了什么事情?
- cookie
- ACM递推递归练习总结
- 第九周:[Leetcode]93. Restore IP Addresses
- 最大字段和