File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > inorderTraversal (TreeNode root ) {
3
+ //iterative
4
+ List <Integer > res = new ArrayList <>();
5
+ if (root == null ) return res ;
6
+
7
+ Stack <TreeNode > stack = new Stack <>();
8
+ TreeNode node = root ;
9
+ while (!stack .isEmpty () || node != null ) {
10
+ if (node != null ) {
11
+ stack .push (node );
12
+ node = node .left ;
13
+ } else {
14
+ node = stack .pop ();
15
+ res .add (node .val );
16
+ node = node .right ;
17
+ }
18
+ }
19
+
20
+ return res ;
21
+
22
+ //recursive
23
+ // List<Integer> res = new ArrayList<>();
24
+ // dfs(root, res);
25
+ // return res;
26
+ }
27
+
28
+ // public void dfs(TreeNode n, List<Integer> res) {
29
+ // if(n == null) {
30
+ // return;
31
+ // }
32
+
33
+ // dfs(n.left, res);
34
+ // res.add(n.val);
35
+ // dfs(n.right, res);
36
+ // }
37
+ }
You can’t perform that action at this time.
0 commit comments