LeetCode 题解(180): Binary Tree Paths
来源:互联网 发布:ubuntu 15.04 下载 编辑:程序博客网 时间:2024/06/05 14:18
题目:
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"]
题解:
递归。
C++版:
class Solution {public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> results; if(root == NULL) return results; string result = to_string(root->val); if(root->left == NULL && root->right == NULL) { results.push_back(result); return results; } traverse(root->left, results, result); traverse(root->right, results, result); return results; } void traverse(TreeNode* root, vector<string>& results, string result) { if(root == NULL) return; if(root->left == NULL && root->right == NULL) { result += "->"; result += to_string(root->val); results.push_back(result); return; } result += "->"; result += to_string(root->val); traverse(root->left, results, result); traverse(root->right, results, result); }};
Java版:
public class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> results = new ArrayList<>(); if(root == null) return results; StringBuffer result = new StringBuffer(Integer.toString(root.val)); if(root.left == null && root.right == null) { results.add(result.toString()); return results; } traverse(root.left, results, result); traverse(root.right, results, result); return results; } public void traverse(TreeNode root, List<String> results, StringBuffer result) { if(root == null) return; if(root.left == null && root.right == null) { StringBuffer temp = new StringBuffer(result.toString()); temp.append("->"); temp.append(Integer.toString(root.val)); results.add(temp.toString()); return; } StringBuffer temp = new StringBuffer(result.toString()); temp.append("->"); temp.append(Integer.toString(root.val)); traverse(root.left, results, temp); traverse(root.right, results, temp); }}
Python版:
class Solution: # @param {TreeNode} root # @return {string[]} def binaryTreePaths(self, root): if root == None: return [] results, result = [], str(root.val) if root.left == None and root.right == None: results.append(result) return results self.traverse(root.left, results, result) self.traverse(root.right, results, result) return results def traverse(self, root, results, result): if root == None: return if root.left == None and root.right == None: temp = result temp += ("->" + str(root.val)) results.append(temp) temp = result temp += ("->" + str(root.val)) self.traverse(root.left, results, temp) self.traverse(root.right, results, temp)
0 0
- LeetCode 题解(180): Binary Tree Paths
- LeetCode题解:Binary Tree Paths
- LeetCode题解:Binary Tree Paths
- LeetCode题解:Binary Tree Paths
- LeetCode题解-257-Binary Tree Paths
- LeetCode 257. Binary Tree Paths 题解
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- [leetcode] Binary Tree Paths
- Leetcode: Binary Tree Paths
- LeetCode:Binary Tree Paths
- leetcode Binary Tree Paths
- leetcode Binary Tree Paths
- LeetCode:Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode Binary Tree Paths
- Python学习笔记:Python函数参数
- VC的编译器cl用法初步
- 《Node入门》读书笔记——用Node.js开发一个小应用
- hdu 5392-Infoplane in Tina Town(质因子分解求最小公倍数)
- HDU 2111 Saving HDU(贪心,01背包)
- LeetCode 题解(180): Binary Tree Paths
- HDU 2112 HDU Today(最短路 SPFA Floyd-Warshall算法)
- oracle中的左右外连接
- 对象,类,方法的自我理解
- 面试准备
- 天声人語 20150816
- android-音乐播放器实现及源码下载(四)
- 高德地图之定位篇----->定位、预测天气、围栏、搜索周边、行踪轨迹
- 选择结构