257:二叉树的所有路径
This commit is contained in:
parent
ce8c27c625
commit
7271b462c7
@ -26,40 +26,49 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
//257:二叉树的所有路径
|
//257:二叉树的所有路径
|
||||||
public class BinaryTreePaths{
|
public class BinaryTreePaths {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//测试代码
|
//测试代码
|
||||||
Solution solution = new BinaryTreePaths().new Solution();
|
Solution solution = new BinaryTreePaths().new Solution();
|
||||||
}
|
}
|
||||||
//力扣代码
|
//力扣代码
|
||||||
//leetcode submit region begin(Prohibit modification and deletion)
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
/**
|
|
||||||
* Definition for a binary tree node.
|
/**
|
||||||
* public class TreeNode {
|
* Definition for a binary tree node.
|
||||||
* int val;
|
* public class TreeNode {
|
||||||
* TreeNode left;
|
* int val;
|
||||||
* TreeNode right;
|
* TreeNode left;
|
||||||
* TreeNode() {}
|
* TreeNode right;
|
||||||
* TreeNode(int val) { this.val = val; }
|
* TreeNode() {}
|
||||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
* TreeNode(int val) { this.val = val; }
|
||||||
* this.val = val;
|
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||||
* this.left = left;
|
* this.val = val;
|
||||||
* this.right = right;
|
* this.left = left;
|
||||||
* }
|
* this.right = right;
|
||||||
* }
|
* }
|
||||||
*/
|
* }
|
||||||
class Solution {
|
*/
|
||||||
public List<String> binaryTreePaths(TreeNode root) {
|
class Solution {
|
||||||
if(root==null){
|
public List<String> binaryTreePaths(TreeNode root) {
|
||||||
return null;
|
List<String> paths = new ArrayList<String>();
|
||||||
|
dfs(root, "", paths);
|
||||||
|
return paths;
|
||||||
}
|
}
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
if(root.left!=null){}
|
|
||||||
|
|
||||||
|
public void dfs(TreeNode root, String path, List<String> paths) {
|
||||||
return list;
|
if (root != null) {
|
||||||
|
path += root.val;
|
||||||
|
if (root.left == null && root.right == null) {
|
||||||
|
paths.add(path);
|
||||||
|
} else {
|
||||||
|
path += "->";
|
||||||
|
dfs(root.left, path, paths);
|
||||||
|
dfs(root.right, path, paths);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
//leetcode submit region end(Prohibit modification and deletion)
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
}
|
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user