File tree Expand file tree Collapse file tree 3 files changed +133
-0
lines changed
solution/0200-0299/0285.Inorder Successor in BST Expand file tree Collapse file tree 3 files changed +133
-0
lines changed Original file line number Diff line number Diff line change 64
64
``` java
65
65
66
66
```
67
+ ### ** JavaScript**
68
+
69
+ ``` js
70
+ /**
71
+ * Definition for a binary tree node.
72
+ * function TreeNode(val) {
73
+ * this.val = val;
74
+ * this.left = this.right = null;
75
+ * }
76
+ */
77
+ /**
78
+ * @param {TreeNode} root
79
+ * @param {TreeNode} p
80
+ * @return {TreeNode}
81
+ */
82
+ var inorderSuccessor = function (root , p ) {
83
+ function findMin (root ) {
84
+ if (! root) {
85
+ return null ;
86
+ }
87
+ while (root .left ) {
88
+ root = root .left ;
89
+ }
90
+ return root;
91
+ }
92
+ if (! root) {
93
+ return null ;
94
+ }
95
+ let successor = null ;
96
+ while (root) {
97
+ if (root .val > p .val ) {
98
+ successor = root;
99
+ root = root .left ;
100
+ } else if (root .val < p .val ) {
101
+ root = root .right ;
102
+ } else {
103
+ if (root .right ) {
104
+ successor = findMin (root .right );
105
+ }
106
+ break ;
107
+ }
108
+ }
109
+ return successor;
110
+ };
111
+ ```
112
+
67
113
68
114
### ** ...**
69
115
Original file line number Diff line number Diff line change 51
51
52
52
```
53
53
54
+ ### ** JavaScript**
55
+
56
+ ``` js
57
+ /**
58
+ * Definition for a binary tree node.
59
+ * function TreeNode(val) {
60
+ * this.val = val;
61
+ * this.left = this.right = null;
62
+ * }
63
+ */
64
+ /**
65
+ * @param {TreeNode} root
66
+ * @param {TreeNode} p
67
+ * @return {TreeNode}
68
+ */
69
+ var inorderSuccessor = function (root , p ) {
70
+ function findMin (root ) {
71
+ if (! root) {
72
+ return null ;
73
+ }
74
+ while (root .left ) {
75
+ root = root .left ;
76
+ }
77
+ return root;
78
+ }
79
+ if (! root) {
80
+ return null ;
81
+ }
82
+ let successor = null ;
83
+ while (root) {
84
+ if (root .val > p .val ) {
85
+ successor = root;
86
+ root = root .left ;
87
+ } else if (root .val < p .val ) {
88
+ root = root .right ;
89
+ } else {
90
+ if (root .right ) {
91
+ successor = findMin (root .right );
92
+ }
93
+ break ;
94
+ }
95
+ }
96
+ return successor;
97
+ };
98
+ ```
99
+
54
100
### ** ...**
55
101
56
102
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {TreeNode } root
10
+ * @param {TreeNode } p
11
+ * @return {TreeNode }
12
+ */
13
+ var inorderSuccessor = function ( root , p ) {
14
+ function findMin ( root ) {
15
+ if ( ! root ) {
16
+ return null ;
17
+ }
18
+ while ( root . left ) {
19
+ root = root . left ;
20
+ }
21
+ return root ;
22
+ }
23
+ if ( ! root ) {
24
+ return null ;
25
+ }
26
+ let successor = null ;
27
+ while ( root ) {
28
+ if ( root . val > p . val ) {
29
+ successor = root ;
30
+ root = root . left ;
31
+ } else if ( root . val < p . val ) {
32
+ root = root . right ;
33
+ } else {
34
+ if ( root . right ) {
35
+ successor = findMin ( root . right ) ;
36
+ }
37
+ break ;
38
+ }
39
+ }
40
+ return successor ;
41
+ } ;
You can’t perform that action at this time.
0 commit comments