File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -56,20 +56,23 @@ var inorderTraversal = function (root) {
56
56
c) Go to step 3.
57
57
5) If current is NULL and stack is empty then we are done.
58
58
*/
59
- var stack = [ ] , ret = [ ] ;
59
+ var ret = [ ] ;
60
+ var stack = [ root ] ;
60
61
var cur = root ;
61
62
62
63
stack . push ( cur ) ;
63
- while ( stack . length > 0 || cur !== null ) {
64
+ while ( stack . length > 0 ) {
64
65
if ( cur && cur . left ) {
65
- stack . push ( cur . left ) ;
66
66
cur = cur . left ;
67
- } else if ( stack . length > 0 ) {
68
- var tmp = stack . pop ( ) ;
69
- ret . push ( tmp . val ) ;
70
- cur = tmp . right ;
71
- if ( cur !== null ) {
72
- stack . push ( cur ) ;
67
+ stack . push ( cur ) ;
68
+ } else {
69
+ if ( stack . length > 0 ) {
70
+ var temp = stack . pop ( ) ;
71
+ ret . push ( temp . val ) ;
72
+ cur = temp . right ;
73
+ if ( cur ) {
74
+ stack . push ( cur ) ;
75
+ }
73
76
}
74
77
}
75
78
}
You can’t perform that action at this time.
0 commit comments