Skip to content

Commit 2dc5684

Browse files
committed
ok
1 parent 11346d2 commit 2dc5684

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

src/com/leetcode/Main257.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 257. 二叉树的所有路径
8+
* 给定一个二叉树,返回所有从根节点到叶子节点的路径。
9+
*
10+
* 说明: 叶子节点是指没有子节点的节点。
11+
*
12+
* 示例:
13+
*
14+
* 输入:
15+
*
16+
* 1
17+
* / \
18+
* 2 3
19+
* \
20+
* 5
21+
*
22+
* 输出: ["1->2->5", "1->3"]
23+
*
24+
* 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
25+
26+
*/
27+
public class Main257 {
28+
public static class TreeNode {
29+
int val;
30+
TreeNode left;
31+
TreeNode right;
32+
TreeNode(int x) { val = x; }
33+
}
34+
public List<String> list = new ArrayList<>();
35+
public void search(TreeNode root, String str) {
36+
if (root.left == null && root.right == null) {
37+
str += root.val;
38+
list.add(str);
39+
return;
40+
}
41+
str += root.val + "->";
42+
if (root.left != null) {
43+
search(root.left, str);
44+
}
45+
if (root.right != null) {
46+
search(root.right, str);
47+
}
48+
}
49+
50+
public List<String> binaryTreePaths(TreeNode root) {
51+
if (root == null)
52+
return new ArrayList<>();
53+
search(root, "");
54+
return list;
55+
}
56+
57+
public static void main(String[] args) {
58+
TreeNode root = new TreeNode(1);
59+
root.left = new TreeNode(2);
60+
root.right = new TreeNode(3);
61+
root.left.left = new TreeNode(4);
62+
Main257 m = new Main257();
63+
List<String> list = m.binaryTreePaths(root);
64+
System.out.println(list);
65+
66+
}
67+
}

src/com/leetcode/Sort.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,6 @@ public void heapify(int[] nums, int curNode, int heapSize) {
192192
heapify(nums, largest, heapSize);
193193
}
194194
}
195-
196-
// public void swap(int[] nums, int x, int y) {
197-
// int tmp = nums[x];
198-
// nums[x] = nums[y];
199-
// nums[y] = tmp;
200-
// }
201195
/********************************堆排序*******************************************/
202196

203197
public static void main(String[] args) {

0 commit comments

Comments
 (0)