File tree 2 files changed +34
-6
lines changed
2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ class TreeNode {
12
12
* @return {number }
13
13
*/
14
14
var kthSmallest = function ( root , k ) {
15
- let list = [ ] , kValue = null , parents = [ ] ;
15
+ let list = [ ] , kValue = null , parents = [ ] ; // stack
16
16
17
17
const inOrder = ( node ) => {
18
18
console . log ( "parents" )
@@ -42,6 +42,37 @@ var kthSmallest = function (root, k) {
42
42
43
43
return kValue ;
44
44
} ;
45
+ // var kthSmallest = function (root, k) {
46
+ // let list = [], kValue = null, parents = [];
47
+ //
48
+ // const inOrder = (node) => {
49
+ // console.log("parents")
50
+ // console.log(parents)
51
+ // if (node === null || k === 0) return;
52
+ //
53
+ // if (node.left !== null) { // going left
54
+ // parents.push(node);
55
+ // inOrder(node.left);
56
+ // } else { // returning to right
57
+ // k--;
58
+ // kValue = node.val;
59
+ // list.push(node.val);
60
+ //
61
+ // if (node.right === null && parents.length > 0) {
62
+ // console.log("r null")
63
+ // let parent = parents.pop();
64
+ // parent.left = null;
65
+ // inOrder(parent);
66
+ // } else { // going to right if there is a right node otherwise we go to the the last parent
67
+ // inOrder(node.right);
68
+ // }
69
+ // }
70
+ // }
71
+ //
72
+ // inOrder(root);
73
+ //
74
+ // return kValue;
75
+ // };
45
76
46
77
47
78
let x =
Original file line number Diff line number Diff line change @@ -15,19 +15,16 @@ var kthSmallest = function (root, k) {
15
15
let kValue = null , parents = [ ] ;
16
16
17
17
const inOrder = ( node ) => {
18
- console . log ( "parents" )
19
- console . log ( parents )
20
18
if ( node === null || k === 0 ) return ;
21
19
22
- if ( node . left !== null ) { // going left
20
+ if ( node . left !== null ) {
23
21
parents . push ( node ) ;
24
22
inOrder ( node . left ) ;
25
- } else { // returning to right
23
+ } else {
26
24
k -- ;
27
25
kValue = node . val ;
28
26
29
27
if ( node . right === null && parents . length > 0 ) {
30
- console . log ( "r null" )
31
28
let parent = parents . pop ( ) ;
32
29
parent . left = null ;
33
30
inOrder ( parent ) ;
You can’t perform that action at this time.
0 commit comments