Skip to content

Commit 6cecaff

Browse files
author
王俊超
committed
commit
1 parent eb56524 commit 6cecaff

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Created by .ignore support plugin (hsz.mobi)
2-
out
2+
out
33
.idea
44
*.iml

Diff for: .idea/modules.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: 【257】【Binary Tree Paths】/src/Solution.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.LinkedList;
2+
import java.util.List;
3+
4+
/**
5+
* <pre>
6+
* Definition for a binary tree node.
7+
* public class TreeNode {
8+
* int val;
9+
* TreeNode left;
10+
* TreeNode right;
11+
* TreeNode(int x) { val = x; }
12+
* }
13+
* </pre>
14+
*
15+
* @author: wangjunchao(王俊超)
16+
* @time: 2018-10-10 11:16
17+
**/
18+
public class Solution {
19+
public List<String> binaryTreePaths(TreeNode root) {
20+
List<String> result = new LinkedList<>();
21+
binaryTreePaths(root, new LinkedList<>(), result);
22+
23+
return result;
24+
}
25+
26+
private void binaryTreePaths(TreeNode root, LinkedList<TreeNode> path, List<String> result) {
27+
28+
if (root == null) {
29+
return;
30+
}
31+
32+
path.add(root);
33+
34+
if (root.left == null && root.right == null) {
35+
addResult(path, result);
36+
} else {
37+
binaryTreePaths(root.left, path, result);
38+
binaryTreePaths(root.right, path, result);
39+
}
40+
41+
path.remove(path.size() - 1);
42+
}
43+
44+
private void addResult(LinkedList<TreeNode> path, List<String> result) {
45+
StringBuilder builder = new StringBuilder();
46+
if (path != null) {
47+
for (TreeNode node : path) {
48+
builder.append("->").append(node.val);
49+
}
50+
}
51+
52+
if (builder.length() > 0) {
53+
result.add(builder.substring(2));
54+
}
55+
}
56+
}

Diff for: 【257】【Binary Tree Paths】/src/Test.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-10-10 11:25
4+
**/
5+
public class Test {
6+
public static void main(String[] args) {
7+
Solution solution = new Solution();
8+
9+
TreeNode root = new TreeNode(1);
10+
root.left = new TreeNode(2);
11+
root.right = new TreeNode(3);
12+
root.left.right = new TreeNode(5);
13+
14+
System.out.println(solution.binaryTreePaths(root));
15+
}
16+
}

Diff for: 【257】【Binary Tree Paths】/src/TreeNode.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-06-18
4+
* Time: 10:02
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
12+
TreeNode(int x) {
13+
val = x;
14+
}
15+
}

Diff for: 【258】【Add Digits】/src/Solution.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-10-10 13:28
4+
**/
5+
public class Solution {
6+
public int addDigits(int num) {
7+
8+
if (num < 0) {
9+
throw new IllegalArgumentException("input num :" + num + ", must not be negative");
10+
}
11+
12+
int result = 0;
13+
14+
while (num != 0) {
15+
result += num % 10;
16+
num /= 10;
17+
18+
if (num == 0 && result > 9) {
19+
num = result;
20+
result = 0;
21+
}
22+
}
23+
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)