5
5
//! * 中等
6
6
//! * [98. 验证二叉搜索树](is_valid_bst)
7
7
//! * [98. 验证二叉搜索树](is_valid_bst_2)
8
+ //! * [2476. 二叉搜索树最近节点查询](closest_nodes)
8
9
//! * 困难
9
10
10
11
use datastructure:: TreeNode ;
@@ -64,14 +65,16 @@ pub fn is_valid_bst_2(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
64
65
let ( l, lmin, lmax, lnone) = inorder ( inner. borrow ( ) . left . clone ( ) ) ;
65
66
if !l {
66
67
return ( false , 0 , 0 , false ) ;
67
- } else if !lnone && v <= lmax {
68
+ }
69
+ if !lnone && v <= lmax {
68
70
return ( false , 0 , 0 , false ) ;
69
71
}
70
72
71
73
let ( r, rmin, rmax, rnone) = inorder ( inner. borrow ( ) . right . clone ( ) ) ;
72
74
if !r {
73
75
return ( false , 0 , 0 , false ) ;
74
- } else if !rnone && v >= rmin {
76
+ }
77
+ if !rnone && v >= rmin {
75
78
return ( false , 0 , 0 , false ) ;
76
79
}
77
80
@@ -82,11 +85,82 @@ pub fn is_valid_bst_2(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
82
85
flag
83
86
}
84
87
88
+ /// [2476. 二叉搜索树最近节点查询](https://leetcode.cn/problems/closest-nodes-queries-in-a-binary-search-tree/)
89
+ ///
90
+ /// ## 思路2: 利用二茬搜索树的性质, 递归. 实际不可行, 因为题目没有保证树的平衡性, 会超时
91
+ /// ## 思路1: 展开成有序数组, 二分查找
92
+ pub fn closest_nodes ( root : Option < Rc < RefCell < TreeNode > > > , queries : Vec < i32 > ) -> Vec < Vec < i32 > > {
93
+ fn inorder ( store : & mut Vec < i32 > , node : Option < Rc < RefCell < TreeNode > > > ) {
94
+ if node. is_none ( ) {
95
+ return ;
96
+ }
97
+ let inner = node. unwrap ( ) . clone ( ) ;
98
+
99
+ inorder ( store, inner. borrow ( ) . left . clone ( ) ) ;
100
+ store. push ( inner. borrow ( ) . val ) ;
101
+ inorder ( store, inner. borrow ( ) . right . clone ( ) ) ;
102
+ }
103
+ let mut store = vec ! [ ] ;
104
+ inorder ( & mut store, root) ;
105
+
106
+ let mut result = vec ! [ ] ;
107
+ for q in queries {
108
+ match store. binary_search ( & q) {
109
+ Ok ( _) => {
110
+ result. push ( vec ! [ q, q] ) ;
111
+ continue ;
112
+ }
113
+ Err ( idx) => {
114
+ if idx == 0 {
115
+ result. push ( vec ! [ -1 , store[ 0 ] ] ) ;
116
+ } else if idx == store. len ( ) {
117
+ result. push ( vec ! [ store[ store. len( ) - 1 ] , -1 ] ) ;
118
+ } else {
119
+ result. push ( vec ! [ store[ idx - 1 ] , store[ idx] ] ) ;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ result
125
+ }
126
+
85
127
#[ cfg( test) ]
86
128
mod tests {
129
+ use crate :: vec2;
130
+
87
131
use super :: * ;
88
132
use macros:: tree;
89
133
134
+ #[ test]
135
+ fn test_closest_nodes ( ) {
136
+ struct TestCase {
137
+ tree : Option < Rc < RefCell < TreeNode > > > ,
138
+ queries : Vec < i32 > ,
139
+ expected : Vec < Vec < i32 > > ,
140
+ }
141
+
142
+ vec ! [
143
+ TestCase {
144
+ tree: tree!{ val: 6 , left: { val: 2 , left: { val: 1 } , right: { val: 4 } } , right: { val: 13 , left: { val: 9 } , right: { val: 15 , left: { val: 14 } } } } ,
145
+ queries: vec![ 2 , 5 , 16 ] ,
146
+ expected: vec![ vec![ 2 , 2 ] , vec![ 4 , 6 ] , vec![ 15 , -1 ] ] ,
147
+ } ,
148
+ TestCase {
149
+ tree: tree!{ val: 4 , right: { val: 9 } } ,
150
+ queries: vec![ 3 ] ,
151
+ expected: vec![ vec![ -1 , 4 ] ] ,
152
+ } ,
153
+ TestCase {
154
+ tree: tree!{ val: 16 , left: { val: 8 , left: { val: 1 , right: { val: 2 , right: { val: 7 } } } , right: { val: 12 , left: { val: 9 } } } , right: { val: 18 , right: { val: 20 } } } ,
155
+ queries: vec![ 8 , 14 , 285508 , 6 ] ,
156
+ expected: vec2![ [ 8 , 8 ] , [ 12 , 16 ] , [ 20 , -1 ] , [ 2 , 7 ] ] ,
157
+ }
158
+ ] . into_iter ( ) . enumerate ( ) . for_each ( |( idx, TestCase { tree, queries, expected} ) |{
159
+ let result = closest_nodes ( tree, queries) ;
160
+ assert_eq ! ( result, expected, "index: {}" , idx) ;
161
+ } )
162
+ }
163
+
90
164
#[ test]
91
165
fn test_is_valid_bst_2 ( ) {
92
166
struct Testcase {
@@ -100,9 +174,8 @@ mod tests {
100
174
expect: true ,
101
175
} ,
102
176
Testcase {
103
- tree:
104
- tree!( { 5 , left: { 1 } , right: { 4 , left: { 3 } , right: { 6 } } } ) ,
105
-
177
+ tree: tree!( { 5 , left: { 1 } , right: { 4 , left: { 3 } , right: { 6 } } } ) ,
178
+
106
179
expect: false ,
107
180
} ,
108
181
Testcase {
@@ -132,9 +205,8 @@ mod tests {
132
205
expect: true ,
133
206
} ,
134
207
Testcase {
135
- tree:
136
- tree!( { 5 , left: { 1 } , right: { 4 , left: { 3 } , right: { 6 } } } ) ,
137
-
208
+ tree: tree!( { 5 , left: { 1 } , right: { 4 , left: { 3 } , right: { 6 } } } ) ,
209
+
138
210
expect: false ,
139
211
} ,
140
212
]
@@ -151,7 +223,7 @@ mod tests {
151
223
fn test_inorder_traversal ( ) {
152
224
struct Testcase {
153
225
tree : Option < Rc < RefCell < TreeNode > > > ,
154
- expect : Vec < i32 > ,
226
+ expect : Vec < i32 > ,
155
227
}
156
228
157
229
vec ! [
@@ -171,7 +243,7 @@ mod tests {
171
243
. into_iter ( )
172
244
. enumerate ( )
173
245
. for_each ( |( idx, testcase) | {
174
- let Testcase { tree, expect } = testcase;
246
+ let Testcase { tree, expect } = testcase;
175
247
let acutal = inorder_traversal ( tree) ;
176
248
assert_eq ! ( expect, acutal, "case {} failed" , idx) ;
177
249
} ) ;
0 commit comments