File tree 1 file changed +8
-10
lines changed
1 file changed +8
-10
lines changed Original file line number Diff line number Diff line change 15
15
*/
16
16
class Solution {
17
17
public List <Integer > inorderTraversal (TreeNode root ) {
18
- List <Integer > list = new ArrayList <>();
19
18
if (root == null ) {
20
- return list ;
19
+ return List . of () ;
21
20
}
22
21
Stack <TreeNode > stack = new Stack <>();
23
- stack .push (root );
24
- root = root .left ;
25
22
while (root != null ) {
26
23
stack .push (root );
27
24
root = root .left ;
28
25
}
26
+ List <Integer > result = new ArrayList <>();
29
27
while (!stack .isEmpty ()) {
30
28
TreeNode removed = stack .pop ();
31
- list .add (removed .val );
32
- removed = removed .right ;
33
- while (removed != null ) {
34
- stack .push (removed );
35
- removed = removed .left ;
29
+ result .add (removed .val );
30
+ TreeNode rightNode = removed .right ;
31
+ while (rightNode != null ) {
32
+ stack .push (rightNode );
33
+ rightNode = rightNode .left ;
36
34
}
37
35
}
38
- return list ;
36
+ return result ;
39
37
}
40
38
}
You can’t perform that action at this time.
0 commit comments