Skip to content

Commit 59af695

Browse files
author
Botao Xiao
committed
[Function add]: 1.Add leetcode solutions.
1 parent 127709e commit 59af695

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@
427427

428428
[242. Valid Anagram](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/242.%20Valid%20Anagram.md)
429429

430+
[257. Binary Tree Paths](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/257.%20Binary%20Tree%20Paths.md)
431+
432+
[258. Add Digits](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/258.%20Add%20Digits.md)
433+
430434
## Algorithm(4th_Edition)
431435
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
432436
All java realization codes are placed in different packages.

leetcode/257. Binary Tree Paths.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,52 @@ class Solution {
5353
}
5454
}
5555
}
56-
```
56+
```
57+
58+
### Second time
59+
1. I still used bfs to solve this question. However, using list instead of StringBuilder is faster.
60+
```Java
61+
/**
62+
* Definition for a binary tree node.
63+
* public class TreeNode {
64+
* int val;
65+
* TreeNode left;
66+
* TreeNode right;
67+
* TreeNode(int x) { val = x; }
68+
* }
69+
*/
70+
class Solution {
71+
private String arrow = "->";
72+
public List<String> binaryTreePaths(TreeNode root) {
73+
List<String> result = new LinkedList<>();
74+
if(root == null) return result;
75+
addPath(result, new LinkedList<Integer>(), root);
76+
return result;
77+
}
78+
private void addPath(List<String> result, List<Integer> temp, TreeNode node){
79+
temp.add(node.val);
80+
if(node.left == null && node.right == null){
81+
if(temp.size() == 0) return;
82+
else if(temp.size() == 1){
83+
result.add("" + temp.get(0));
84+
}else{
85+
StringBuilder sb = new StringBuilder();
86+
sb.append(temp.get(0));
87+
for(int i = 1; i < temp.size(); i++){
88+
sb.append(this.arrow);
89+
sb.append(temp.get(i));
90+
}
91+
result.add(sb.toString());
92+
}
93+
}else{
94+
if(node.left != null){
95+
addPath(result, temp, node.left);
96+
}
97+
if(node.right != null){
98+
addPath(result, temp, node.right);
99+
}
100+
}
101+
temp.remove(temp.size() - 1);
102+
}
103+
}
104+
```

leetcode/258. Add Digits.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Given a non-negative integer num, repeatedly add all its digits until the result
77
Example:
88
99
Input: 38
10-
Output: 2
11-
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
10+
Output: 2
11+
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
1212
Since 2 has only one digit, return it.
1313
```
1414

@@ -38,4 +38,20 @@ class Solution {
3838
return (num - 1)%9 + 1;
3939
}
4040
}
41-
```
41+
```
42+
43+
### Second time
44+
1. Recursion
45+
```Java
46+
class Solution {
47+
public int addDigits(int num) {
48+
if(num <= 9) return num;
49+
int sum = 0;
50+
while(num > 0){
51+
sum += num % 10;
52+
num /= 10;
53+
}
54+
return addDigits(sum);
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)