Skip to content

Commit 094a9dc

Browse files
committed
0094. Binary Tree Inorder Traversal
1 parent 1741575 commit 094a9dc

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@
6767
* [0091. Decode Ways](markdown/0091.%20Decode%20Ways)
6868
* [0092. Reverse Linked List II](markdown/0092.%20Reverse%20Linked%20List%20II)
6969
* [0093. Restore IP Addresses](markdown/0093.%20Restore%20IP%20Addresses)
70+
* [0094. Binary Tree Inorder Traversal](markdown/0094.%20Binary%20Tree%20Inorder%20Traversal)
7071
* [0146. LRU Cache](markdown/0146.%20LRU%20Cache)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### [94\. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a binary tree, return the _inorder_ traversal of its nodes' values.
7+
8+
**Example:**
9+
10+
```
11+
Input: [1,null,2,3]
12+
1
13+
\
14+
2
15+
/
16+
3
17+
18+
Output: [1,3,2]
19+
```
20+
21+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
/**
30+
* Definition for a binary tree node.
31+
* public class TreeNode {
32+
*     int val;
33+
*     TreeNode left;
34+
*     TreeNode right;
35+
*     TreeNode(int x) { val = x; }
36+
* }
37+
*/
38+
class Solution {
39+
   public List<Integer> inorderTraversal(TreeNode root) {
40+
       Stack<TreeNode> stack = new Stack<>();
41+
       List<Integer> result = new ArrayList<>();
42+
43+
       TreeNode p = root;
44+
       while (p != null || !stack.isEmpty()) {
45+
           // 一直向左,把所有节点压栈
46+
           while (p != null) {
47+
               stack.push(p);
48+
               p = p.left;
49+
          }
50+
           // 压栈结束,元素出栈
51+
           p = stack.pop();
52+
           // 打印 value
53+
           result.add(p.val);
54+
           // 对右子树做同样的操作
55+
           p = p.right;
56+
      }
57+
       return result;
58+
  }
59+
}
60+
```
61+
![LH5Tf6](https://cdn.jsdelivr.net/gh/PicGoBed/PicBed@master/uPic/LH5Tf6.png)

src/main/java/leetcode/_94_/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode._94_;
2+
3+
import leetcode.common.TreeNode;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
12+
TreeNode root = new TreeNode(1);
13+
TreeNode n2 = new TreeNode(2);
14+
TreeNode n3 = new TreeNode(3);
15+
root.right = n2;
16+
n2.left = n3;
17+
System.out.println(solution.inorderTraversal(root));
18+
}
19+
}
20+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode._94_;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
import leetcode.common.TreeNode;
8+
9+
class Solution {
10+
public List<Integer> inorderTraversal(TreeNode root) {
11+
Stack<TreeNode> stack = new Stack<>();
12+
List<Integer> result = new ArrayList<>();
13+
14+
TreeNode p = root;
15+
while (p != null || !stack.isEmpty()) {
16+
// 一直向左,把所有节点压栈
17+
while (p != null) {
18+
stack.push(p);
19+
p = p.left;
20+
}
21+
// 压栈结束,元素出栈
22+
p = stack.pop();
23+
// 打印 value
24+
result.add(p.val);
25+
// 对右子树做同样的操作
26+
p = p.right;
27+
}
28+
return result;
29+
}
30+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### [94\. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a binary tree, return the _inorder_ traversal of its nodes' values.
7+
8+
**Example:**
9+
10+
```
11+
Input: [1,null,2,3]
12+
1
13+
\
14+
2
15+
/
16+
3
17+
18+
Output: [1,3,2]
19+
```
20+
21+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
/**
30+
* Definition for a binary tree node.
31+
* public class TreeNode {
32+
*     int val;
33+
*     TreeNode left;
34+
*     TreeNode right;
35+
*     TreeNode(int x) { val = x; }
36+
* }
37+
*/
38+
class Solution {
39+
   public List<Integer> inorderTraversal(TreeNode root) {
40+
       Stack<TreeNode> stack = new Stack<>();
41+
       List<Integer> result = new ArrayList<>();
42+
43+
       TreeNode p = root;
44+
       while (p != null || !stack.isEmpty()) {
45+
           // 一直向左,把所有节点压栈
46+
           while (p != null) {
47+
               stack.push(p);
48+
               p = p.left;
49+
          }
50+
           // 压栈结束,元素出栈
51+
           p = stack.pop();
52+
           // 打印 value
53+
           result.add(p.val);
54+
           // 对右子树做同样的操作
55+
           p = p.right;
56+
      }
57+
       return result;
58+
  }
59+
}
60+
```
61+
![LH5Tf6](https://cdn.jsdelivr.net/gh/PicGoBed/PicBed@master/uPic/LH5Tf6.png)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode.common;
2+
3+
/**
4+
* Created by jacob on 2020/2/29.
5+
*/
6+
public class TreeNode {
7+
public int val;
8+
public TreeNode left;
9+
public TreeNode right;
10+
11+
public TreeNode(int x) { val = x; }
12+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy