【LeetCode】Binary Tree Paths 解题报告(java & python)
2017-05-06 11:12
465 查看
【LeetCode】Binary Tree Paths 解题报告(java & python)
标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/binary-tree-paths/#/description
题目描述:
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:
Example:
1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"]
Ways
把二叉树的从根节点到叶子节点的每条路径都打印出来,实用的方法就是很简单的递归调用。如果是叶子就把这个路径保存到list中,如果不是叶子就把这个节点的值放入到path中,然后再继续调用,直到达到叶子节点为止。我用StringBuilder的结果会糅杂在一起,就不能用,也没想明白为什么= =
java版本:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> ans = new ArrayList<String>(); if(root != null){ searchNode(root, "", ans); } return ans; } public void searchNode(TreeNode root, String path, List<String> ans){ if(root.left == null && root.right == null){ ans.add(path + root.val); } if(root.left != null){ searchNode(root.left, path + root.val + "->", ans); } if(root.right != null){ searchNode(root.right, path + root.val + "->", ans); } } }
===========二刷
python版本:
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] res = [] self.dfs(root, res, '' + str(root.val)) return res def dfs(self, root, res, path): if root.left == None and root.right == None: res.append(path) if root.left != None: self.dfs(root.left, res, path + '->' + str(root.left.val)) if root.right != None: self.dfs(root.right, res, path + '->' + str(root.right.val))
Date
2017 年 5 月 6 日2018 年 2 月 25 日
相关文章推荐
- 【LeetCode】Validate Binary Search Tree 解题报告(Java & Python)
- 【LeetCode】Linked List Cycle 解题报告(Java & Python)
- 【LeetCode】Single Number 解题报告(Java & Python)
- [leetcode]110. Balanced Binary Tree@Java解题报告
- 【LeetCode】Merge Two Sorted Lists 解题报告(Java & Python)
- Maximum Depth of Binary Tree | leetcode 104 【Java解题报告】
- 【LeetCode】Hamming Distance 解题报告(java & python)
- 【LeetCode】Group Anagrams 解题报告(Python & Java)
- 景岁的Leetcode解题报告:297. Serialize and Deserialize Binary Tree(Python)
- 【LeetCode】Reverse Linked List 解题报告(Python & java)
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
- 【LeetCode】Two Sum 解题报告(java & python)
- [leetcode]98. Validate Binary Search Tree@Java解题报告
- LeetCode(254) Binary Tree Paths解题报告
- LeetCode-Binary Tree Paths -解题报告
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- 【LeetCode】Flatten Binary Tree to Linked List 解题报告
- LeetCode—Binary Tree Preorder Traversal 解题报告