Skip to content

Commit a09d379

Browse files
committed
solve #98
1 parent cce0077 commit a09d379

4 files changed

+89
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ mod n0094_binary_tree_inorder_traversal;
9898
mod n0095_unique_binary_search_trees_ii;
9999
mod n0096_unique_binary_search_trees;
100100
mod n0097_interleaving_string;
101+
mod n0098_validate_binary_search_tree;

src/n0052_n_queens_ii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mod tests {
8686
fn test_52() {
8787
assert_eq!(Solution::total_n_queens(4), 2);
8888
assert_eq!(Solution::total_n_queens(8), 92);
89-
// assert_eq!(Solution::total_n_queens(13), 73712);
89+
assert_eq!(Solution::total_n_queens(13), 73712);
9090
// assert_eq!(Solution::total_n_queens(14), 365596);
9191
}
9292
}

src/n0097_interleaving_string.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct Solution {}
2222

2323
// submission codes start here
2424

25+
// DFS with memorization
26+
2527
impl Solution {
2628
pub fn is_interleave(s1: String, s2: String, s3: String) -> bool {
2729
let mut cache = vec![vec![false;s2.len()+1];s1.len()+1];
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* [98] Validate Binary Search Tree
3+
*
4+
* Given a binary tree, determine if it is a valid binary search tree (BST).
5+
*
6+
* Assume a BST is defined as follows:
7+
*
8+
*
9+
* The left subtree of a node contains only nodes with keys less than the node's key.
10+
* The right subtree of a node contains only nodes with keys greater than the node's key.
11+
* Both the left and right subtrees must also be binary search trees.
12+
*
13+
*
14+
* Example 1:
15+
*
16+
*
17+
* Input:
18+
* 2
19+
* / \
20+
* 1 3
21+
* Output: true
22+
*
23+
*
24+
* Example 2:
25+
*
26+
*
27+
* 5
28+
* / \
29+
* 1 4
30+
* / \
31+
* 3 6
32+
* Output: false
33+
* Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
34+
* is 5 but its right child's value is 4.
35+
*
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
// submission codes start here
41+
42+
// Definition for a binary tree node.
43+
use super::util::tree::{TreeNode, to_tree};
44+
45+
use std::rc::Rc;
46+
use std::cell::RefCell;
47+
impl Solution {
48+
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
49+
let mut vec = vec![];
50+
Solution::preorder_traverse(root.as_ref(), &mut vec)
51+
}
52+
53+
fn preorder_traverse(root: Option<&Rc<RefCell<TreeNode>>>, formers: &mut Vec<(i32,i32)>) -> bool {
54+
if let Some(node) = root {
55+
let root_val = root.as_ref().unwrap().borrow().val;
56+
for former in formers.iter() {
57+
if (former.0 < 0 && root_val >= former.1) ||
58+
(former.0 > 0 && root_val <= former.1) {
59+
return false
60+
}
61+
}
62+
let mut to_right = formers.clone();
63+
formers.push((-1, root_val));
64+
to_right.push((1, root_val));
65+
Solution::preorder_traverse(node.borrow().left.as_ref(), formers) &&
66+
Solution::preorder_traverse(node.borrow().right.as_ref(), &mut to_right)
67+
} else {
68+
true
69+
}
70+
}
71+
}
72+
73+
// submission codes end
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
79+
#[test]
80+
fn test_98() {
81+
assert_eq!(Solution::is_valid_bst(tree![5,1,4,null,null,3,6]), false);
82+
assert_eq!(Solution::is_valid_bst(tree![2,1,3]), true);
83+
assert_eq!(Solution::is_valid_bst(tree![10,5,15,null,null,6,20]), false);
84+
}
85+
}

0 commit comments

Comments
 (0)