Skip to content

Commit 933079e

Browse files
committed
20240224 binary-search-tree inorder
1 parent 1c26837 commit 933079e

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed

src/tree/dfs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ mod tests {
9898
use std::vec;
9999

100100
use super::*;
101-
use datastructure::TreeNode;
102101

103102
#[test]
104103
fn test_distance_k() {

src/tree/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod dfs;
99
mod tests {
1010
#[test]
1111
fn test_macro() {
12-
let t = macros::tree!({val: 1, left: {2, right: {3}}});
12+
let t = macros::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}}});
1313
dbg!(t);
1414
}
1515
}

src/tree/traversal/in_order.rs

+82-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! * 中等
66
//! * [98. 验证二叉搜索树](is_valid_bst)
77
//! * [98. 验证二叉搜索树](is_valid_bst_2)
8+
//! * [2476. 二叉搜索树最近节点查询](closest_nodes)
89
//! * 困难
910
1011
use datastructure::TreeNode;
@@ -64,14 +65,16 @@ pub fn is_valid_bst_2(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
6465
let (l, lmin, lmax, lnone) = inorder(inner.borrow().left.clone());
6566
if !l {
6667
return (false, 0, 0, false);
67-
} else if !lnone && v <= lmax {
68+
}
69+
if !lnone && v <= lmax {
6870
return (false, 0, 0, false);
6971
}
7072

7173
let (r, rmin, rmax, rnone) = inorder(inner.borrow().right.clone());
7274
if !r {
7375
return (false, 0, 0, false);
74-
} else if !rnone && v >= rmin {
76+
}
77+
if !rnone && v >= rmin {
7578
return (false, 0, 0, false);
7679
}
7780

@@ -82,11 +85,82 @@ pub fn is_valid_bst_2(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
8285
flag
8386
}
8487

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+
85127
#[cfg(test)]
86128
mod tests {
129+
use crate::vec2;
130+
87131
use super::*;
88132
use macros::tree;
89133

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+
90164
#[test]
91165
fn test_is_valid_bst_2() {
92166
struct Testcase {
@@ -100,9 +174,8 @@ mod tests {
100174
expect: true,
101175
},
102176
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+
106179
expect: false,
107180
},
108181
Testcase {
@@ -132,9 +205,8 @@ mod tests {
132205
expect: true,
133206
},
134207
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+
138210
expect: false,
139211
},
140212
]
@@ -151,7 +223,7 @@ mod tests {
151223
fn test_inorder_traversal() {
152224
struct Testcase {
153225
tree: Option<Rc<RefCell<TreeNode>>>,
154-
expect:Vec<i32>,
226+
expect: Vec<i32>,
155227
}
156228

157229
vec![
@@ -171,7 +243,7 @@ mod tests {
171243
.into_iter()
172244
.enumerate()
173245
.for_each(|(idx, testcase)| {
174-
let Testcase {tree, expect } = testcase;
246+
let Testcase { tree, expect } = testcase;
175247
let acutal = inorder_traversal(tree);
176248
assert_eq!(expect, acutal, "case {} failed", idx);
177249
});

0 commit comments

Comments
 (0)