257. Binary Tree Paths java solutions

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"]

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     List<String> ans = new ArrayList<String>();
12     public List<String> binaryTreePaths(TreeNode root) {
13         if(root == null) return ans;
14         String tmp = "" + root.val;
15         DFS(ans,root,tmp);
16         return ans;
17     }
18
19     public void DFS(List<String> ans, TreeNode node, String tmp){
20         if(node.left == null && node.right == null){
21             ans.add(tmp);
22             return;
23         }
24         if(node.left != null) DFS(ans,node.left,tmp+"->"+node.left.val);
25         if(node.right != null) DFS(ans,node.right,tmp+"->"+node.right.val);
26     }
27 }

用递归即可。

时间: 2024-08-06 16:06:45

257. Binary Tree Paths java solutions的相关文章

&lt;LeetCode OJ&gt; 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy 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: ["

leetCode 257. Binary Tree Paths 二叉树路径

257. Binary Tree Paths 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"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字

Java [Leetcode 257]Binary Tree Paths

题目描述: 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"] 解题思路: 使用递归法. 代码如下: /** * Definition for a binary tree node. * pu

LeetCode 257. Binary Tree Paths (二叉树路径)

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"] 题目标签:Tree 这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list. 我们可以另外设一个fin

【LeetCode】257. Binary Tree Paths 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51474128 Subject 出处:https://leetcode.com/problems/binary-tree-paths/ Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-le

重做257. Binary Tree Paths

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"] Solution1: 用string是最方便的.因为string是immutable的,所以当返回的时候不会带回最后加的string. /*

257. Binary Tree Paths

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"] 用backtracking的方法.与113题很类似.需要注意其实root元素 public IList<string> Bina

257. Binary Tree Paths [easy] (Python)

题目链接 https://leetcode.com/problems/binary-tree-paths/ 题目原文 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"] 题目翻译 给定一个二叉

257. Binary Tree Paths 二叉树路径

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"] # Definition for a binary tree node. # class TreeNode(object): # def _