Skip to content

Commit 6d9d4fe

Browse files
committed
feat: add solutions to lcci problem: No.04.05
No.04.05.Legal Binary Search Tree
1 parent dfafcab commit 6d9d4fe

File tree

5 files changed

+407
-0
lines changed

5 files changed

+407
-0
lines changed

lcci/04.05.Legal Binary Search Tree/README.md

+153
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,159 @@ func check(node *TreeNode, lower, upper int) bool {
128128
}
129129
```
130130

131+
### **C++**
132+
133+
```cpp
134+
/**
135+
* Definition for a binary tree node.
136+
* struct TreeNode {
137+
* int val;
138+
* TreeNode *left;
139+
* TreeNode *right;
140+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
bool isValidBST(TreeNode *root) {
146+
TreeNode *pre = nullptr;
147+
TreeNode *cur = root;
148+
stack<TreeNode *> stk;
149+
while (cur || !stk.empty()) {
150+
if (cur) {
151+
stk.push(cur);
152+
cur = cur->left;
153+
} else {
154+
cur = stk.top();
155+
stk.pop();
156+
if (pre && pre->val >= cur->val) {
157+
return false;
158+
}
159+
pre = cur;
160+
cur = cur->right;
161+
}
162+
}
163+
return true;
164+
}
165+
};
166+
```
167+
168+
### **TypeScript**
169+
170+
```ts
171+
/**
172+
* Definition for a binary tree node.
173+
* class TreeNode {
174+
* val: number
175+
* left: TreeNode | null
176+
* right: TreeNode | null
177+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
178+
* this.val = (val===undefined ? 0 : val)
179+
* this.left = (left===undefined ? null : left)
180+
* this.right = (right===undefined ? null : right)
181+
* }
182+
* }
183+
*/
184+
185+
function isValidBST(root: TreeNode | null): boolean {
186+
let pre = -Infinity;
187+
const dfs = (root: TreeNode | null) => {
188+
if (root == null) {
189+
return true;
190+
}
191+
const { val, left, right } = root;
192+
if (!dfs(left) || val <= pre) {
193+
return false;
194+
}
195+
pre = val;
196+
return dfs(right);
197+
};
198+
return dfs(root);
199+
}
200+
```
201+
202+
```ts
203+
/**
204+
* Definition for a binary tree node.
205+
* class TreeNode {
206+
* val: number
207+
* left: TreeNode | null
208+
* right: TreeNode | null
209+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
210+
* this.val = (val===undefined ? 0 : val)
211+
* this.left = (left===undefined ? null : left)
212+
* this.right = (right===undefined ? null : right)
213+
* }
214+
* }
215+
*/
216+
217+
function isValidBST(root: TreeNode | null): boolean {
218+
if (root == null) {
219+
return true;
220+
}
221+
const { val, left, right } = root;
222+
const dfs = (root: TreeNode | null, min: number, max: number) => {
223+
if (root == null) {
224+
return true;
225+
}
226+
const { val, left, right } = root;
227+
if (val <= min || val >= max) {
228+
return false;
229+
}
230+
return (
231+
dfs(left, min, Math.min(val, max)) &&
232+
dfs(right, Math.max(val, min), max)
233+
);
234+
};
235+
return dfs(left, -Infinity, val) && dfs(right, val, Infinity);
236+
}
237+
```
238+
239+
### **Rust**
240+
241+
```rust
242+
// Definition for a binary tree node.
243+
// #[derive(Debug, PartialEq, Eq)]
244+
// pub struct TreeNode {
245+
// pub val: i32,
246+
// pub left: Option<Rc<RefCell<TreeNode>>>,
247+
// pub right: Option<Rc<RefCell<TreeNode>>>,
248+
// }
249+
//
250+
// impl TreeNode {
251+
// #[inline]
252+
// pub fn new(val: i32) -> Self {
253+
// TreeNode {
254+
// val,
255+
// left: None,
256+
// right: None
257+
// }
258+
// }
259+
// }
260+
use std::rc::Rc;
261+
use std::cell::RefCell;
262+
impl Solution {
263+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, pre: &mut Option<i32>) -> bool {
264+
if root.is_none() {
265+
return true;
266+
}
267+
let root = root.as_ref().unwrap().borrow();
268+
if !Self::dfs(&root.left, pre) {
269+
return false;
270+
}
271+
if pre.is_some() && pre.unwrap() >= root.val {
272+
return false;
273+
}
274+
*pre = Some(root.val);
275+
Self::dfs(&root.right, pre)
276+
}
277+
278+
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
279+
Self::dfs(&root, &mut None)
280+
}
281+
}
282+
```
283+
131284
### **...**
132285

133286
```

lcci/04.05.Legal Binary Search Tree/README_EN.md

+153
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,159 @@ func check(node *TreeNode, lower, upper int) bool {
157157
}
158158
```
159159

160+
### **C++**
161+
162+
```cpp
163+
/**
164+
* Definition for a binary tree node.
165+
* struct TreeNode {
166+
* int val;
167+
* TreeNode *left;
168+
* TreeNode *right;
169+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
170+
* };
171+
*/
172+
class Solution {
173+
public:
174+
bool isValidBST(TreeNode *root) {
175+
TreeNode *pre = nullptr;
176+
TreeNode *cur = root;
177+
stack<TreeNode *> stk;
178+
while (cur || !stk.empty()) {
179+
if (cur) {
180+
stk.push(cur);
181+
cur = cur->left;
182+
} else {
183+
cur = stk.top();
184+
stk.pop();
185+
if (pre && pre->val >= cur->val) {
186+
return false;
187+
}
188+
pre = cur;
189+
cur = cur->right;
190+
}
191+
}
192+
return true;
193+
}
194+
};
195+
```
196+
197+
### **TypeScript**
198+
199+
```ts
200+
/**
201+
* Definition for a binary tree node.
202+
* class TreeNode {
203+
* val: number
204+
* left: TreeNode | null
205+
* right: TreeNode | null
206+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
207+
* this.val = (val===undefined ? 0 : val)
208+
* this.left = (left===undefined ? null : left)
209+
* this.right = (right===undefined ? null : right)
210+
* }
211+
* }
212+
*/
213+
214+
function isValidBST(root: TreeNode | null): boolean {
215+
let pre = -Infinity;
216+
const dfs = (root: TreeNode | null) => {
217+
if (root == null) {
218+
return true;
219+
}
220+
const { val, left, right } = root;
221+
if (!dfs(left) || val <= pre) {
222+
return false;
223+
}
224+
pre = val;
225+
return dfs(right);
226+
};
227+
return dfs(root);
228+
}
229+
```
230+
231+
```ts
232+
/**
233+
* Definition for a binary tree node.
234+
* class TreeNode {
235+
* val: number
236+
* left: TreeNode | null
237+
* right: TreeNode | null
238+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
239+
* this.val = (val===undefined ? 0 : val)
240+
* this.left = (left===undefined ? null : left)
241+
* this.right = (right===undefined ? null : right)
242+
* }
243+
* }
244+
*/
245+
246+
function isValidBST(root: TreeNode | null): boolean {
247+
if (root == null) {
248+
return true;
249+
}
250+
const { val, left, right } = root;
251+
const dfs = (root: TreeNode | null, min: number, max: number) => {
252+
if (root == null) {
253+
return true;
254+
}
255+
const { val, left, right } = root;
256+
if (val <= min || val >= max) {
257+
return false;
258+
}
259+
return (
260+
dfs(left, min, Math.min(val, max)) &&
261+
dfs(right, Math.max(val, min), max)
262+
);
263+
};
264+
return dfs(left, -Infinity, val) && dfs(right, val, Infinity);
265+
}
266+
```
267+
268+
### **Rust**
269+
270+
```rust
271+
// Definition for a binary tree node.
272+
// #[derive(Debug, PartialEq, Eq)]
273+
// pub struct TreeNode {
274+
// pub val: i32,
275+
// pub left: Option<Rc<RefCell<TreeNode>>>,
276+
// pub right: Option<Rc<RefCell<TreeNode>>>,
277+
// }
278+
//
279+
// impl TreeNode {
280+
// #[inline]
281+
// pub fn new(val: i32) -> Self {
282+
// TreeNode {
283+
// val,
284+
// left: None,
285+
// right: None
286+
// }
287+
// }
288+
// }
289+
use std::rc::Rc;
290+
use std::cell::RefCell;
291+
impl Solution {
292+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, pre: &mut Option<i32>) -> bool {
293+
if root.is_none() {
294+
return true;
295+
}
296+
let root = root.as_ref().unwrap().borrow();
297+
if !Self::dfs(&root.left, pre) {
298+
return false;
299+
}
300+
if pre.is_some() && pre.unwrap() >= root.val {
301+
return false;
302+
}
303+
*pre = Some(root.val);
304+
Self::dfs(&root.right, pre)
305+
}
306+
307+
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
308+
Self::dfs(&root, &mut None)
309+
}
310+
}
311+
```
312+
160313
### **...**
161314

162315
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool isValidBST(TreeNode *root) {
13+
TreeNode *pre = nullptr;
14+
TreeNode *cur = root;
15+
stack<TreeNode *> stk;
16+
while (cur || !stk.empty()) {
17+
if (cur) {
18+
stk.push(cur);
19+
cur = cur->left;
20+
} else {
21+
cur = stk.top();
22+
stk.pop();
23+
if (pre && pre->val >= cur->val) {
24+
return false;
25+
}
26+
pre = cur;
27+
cur = cur->right;
28+
}
29+
}
30+
return true;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, pre: &mut Option<i32>) -> bool {
23+
if root.is_none() {
24+
return true;
25+
}
26+
let root = root.as_ref().unwrap().borrow();
27+
if !Self::dfs(&root.left, pre) {
28+
return false;
29+
}
30+
if pre.is_some() && pre.unwrap() >= root.val {
31+
return false;
32+
}
33+
*pre = Some(root.val);
34+
Self::dfs(&root.right, pre)
35+
}
36+
37+
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
38+
Self::dfs(&root, &mut None)
39+
}
40+
}

0 commit comments

Comments
 (0)