Skip to content

Commit 8ad8453

Browse files
committed
note
1 parent 6019114 commit 8ad8453

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

guangxu/202101/20210128.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Solution {
7676
dp[i+1] += dp[i];
7777
}
7878
// 当前数字前一个数字可以组合对应一个字母
79-
if(i-1 >= 0 && s.charAt(i)!='0' && Integer.parseInt(s.substring(i-1, i+1)) <= 26){
79+
if(i-1 >= 0 && s.charAt(i-1)!='0' && Integer.parseInt(s.substring(i-1, i+1)) <= 26){
8080
dp[i+1] += dp[i-1];
8181
}
8282
}

guangxu/202101/20210129.md

+111-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,121 @@
11
## Algorithm
22

3+
[94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
4+
35
### Description
46

7+
Given the root of a binary tree, return the inorder traversal of its nodes' values.
8+
9+
Example 1:
10+
11+
![](assets/20210129-97802a7b.png)
12+
13+
```
14+
Input: root = [1,null,2,3]
15+
Output: [1,3,2]
16+
```
17+
18+
Example 2:
19+
20+
```
21+
Input: root = []
22+
Output: []
23+
```
24+
25+
Example 3:
26+
27+
```
28+
Input: root = [1]
29+
Output: [1]
30+
```
31+
32+
Example 4:
33+
34+
![](assets/20210129-360b580d.png)
35+
36+
```
37+
Input: root = [1,2]
38+
Output: [2,1]
39+
```
40+
41+
Example 5:
42+
43+
![](assets/20210129-1a3128aa.png)
44+
45+
```
46+
Input: root = [1,null,2]
47+
Output: [1,2]
48+
```
49+
50+
Constraints:
51+
52+
- The number of nodes in the tree is in the range [0, 100].
53+
- -100 <= Node.val <= 100
54+
55+
56+
Follow up:
57+
58+
Recursive solution is trivial, could you do it iteratively?
59+
560
### Solution
661

7-
```java
62+
非递归方法
63+
64+
```java
65+
/**
66+
* Definition for a binary tree node.
67+
* public class TreeNode {
68+
* int val;
69+
* TreeNode left;
70+
* TreeNode right;
71+
* TreeNode() {}
72+
* TreeNode(int val) { this.val = val; }
73+
* TreeNode(int val, TreeNode left, TreeNode right) {
74+
* this.val = val;
75+
* this.left = left;
76+
* this.right = right;
77+
* }
78+
* }
79+
*/
80+
class Solution {
81+
public List<Integer> inorderTraversal(TreeNode root) {
82+
List<Integer> list = new ArrayList<Integer>();
83+
Stack<TreeNode> stack = new Stack<TreeNode>();
84+
while(root!=null || !stack.isEmpty()){
85+
while(root!=null){
86+
stack.push(root);
87+
root = root.left;
88+
}
89+
root = stack.pop();
90+
list.add(root.val);
91+
root = root.right;
92+
}
93+
return list;
94+
}
95+
}
96+
```
97+
98+
递归方法
899

100+
```JAVA
101+
private List<Integer> list = new ArrayList<>();
102+
public List<Integer> inorderTraversal(TreeNode root) {
103+
if(root==null){
104+
return list;
105+
}
106+
inOrder(root);
107+
return list;
108+
}
109+
private void inOrder(TreeNode root){
110+
if(root.left!=null){
111+
inOrder(root.left);
112+
}
113+
list.add(root.val);
114+
if(root.right!=null){
115+
inOrder(root.right);
116+
}
117+
return;
118+
}
9119
```
10120

11121
### Discuss
12.2 KB
Loading
12 KB
Loading
19.4 KB
Loading

guangxu/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| 标题 | Algorithm | Review | Tip | Share|
22
| :-----| :---- | :---- | :---- | :---- |
3+
| [20210129](./202101/20210129.md) |[94. Binary Tree Inorder Traversal(二叉树中序遍历)](https://leetcode.com/problems/binary-tree-inorder-traversal/)||||
34
| [20210128](./202101/20210128.md) |[91. Decode Ways(动态规划解码方法)](https://leetcode.com/problems/decode-ways/)||||
45
| [20210127](./202101/20210127.md) |[48. Rotate Image(图像旋转)](https://leetcode.com/problems/rotate-image)||||
56
| [20210126](./202101/20210126.md) |[215. Kth Largest Element in an Array(数组中第K个最大元素)](https://leetcode.com/problems/kth-largest-element-in-an-array/)||||

0 commit comments

Comments
 (0)