File tree Expand file tree Collapse file tree 6 files changed +185
-0
lines changed Expand file tree Collapse file tree 6 files changed +185
-0
lines changed Original file line number Diff line number Diff line change 67
67
* [ 0091. Decode Ways] ( markdown/0091.%20Decode%20Ways )
68
68
* [ 0092. Reverse Linked List II] ( markdown/0092.%20Reverse%20Linked%20List%20II )
69
69
* [ 0093. Restore IP Addresses] ( markdown/0093.%20Restore%20IP%20Addresses )
70
+ * [ 0094. Binary Tree Inorder Traversal] ( markdown/0094.%20Binary%20Tree%20Inorder%20Traversal )
70
71
* [ 0146. LRU Cache] ( markdown/0146.%20LRU%20Cache )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments