Skip to content

Commit dbd9984

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 04.05. 合法二叉搜索树
1 parent ca2f8d6 commit dbd9984

File tree

4 files changed

+232
-50
lines changed

4 files changed

+232
-50
lines changed

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

+53-1
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,72 @@
66

77
## 解法
88
<!-- 这里可写通用的实现逻辑 -->
9-
9+
一棵合法的二叉搜索树,其中序遍历的结果应该升序排列。
1010

1111
### Python3
1212
<!-- 这里可写当前语言的特殊实现逻辑 -->
1313

1414
```python
15+
# Definition for a binary tree node.
16+
# class TreeNode:
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.left = None
20+
# self.right = None
21+
22+
class Solution:
23+
res, t = True, None
24+
def isValidBST(self, root: TreeNode) -> bool:
25+
self.isValid(root)
26+
return self.res
1527

28+
def isValid(self, root):
29+
if not root:
30+
return
31+
self.isValid(root.left)
32+
if self.t is None or self.t < root.val:
33+
self.t = root.val
34+
else:
35+
self.res = False
36+
return
37+
self.isValid(root.right)
1638
```
1739

1840
### Java
1941
<!-- 这里可写当前语言的特殊实现逻辑 -->
2042

2143
```java
44+
/**
45+
* Definition for a binary tree node.
46+
* public class TreeNode {
47+
* int val;
48+
* TreeNode left;
49+
* TreeNode right;
50+
* TreeNode(int x) { val = x; }
51+
* }
52+
*/
53+
class Solution {
54+
private boolean res = true;
55+
private Integer t = null;
56+
public boolean isValidBST(TreeNode root) {
57+
isValid(root);
58+
return res;
59+
}
2260

61+
private void isValid(TreeNode root) {
62+
if (root == null) {
63+
return;
64+
}
65+
isValid(root.left);
66+
if (t == null || t < root.val) {
67+
t = root.val;
68+
} else {
69+
res = false;
70+
return;
71+
}
72+
isValid(root.right);
73+
}
74+
}
2375
```
2476

2577
### ...
+125-49
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,125 @@
1-
# [04.05. Legal Binary Search Tree](https://leetcode-cn.com/problems/legal-binary-search-tree-lcci)
2-
3-
## Description
4-
<p>Implement a function to check if a binary tree is a binary search tree.</p>
5-
6-
<p><strong>Example&nbsp;1:</strong></p>
7-
8-
<pre>
9-
<strong>Input:</strong>
10-
2
11-
/ \
12-
1 3
13-
<strong>Output:</strong> true
14-
</pre>
15-
16-
<p><strong>Example&nbsp;2:</strong></p>
17-
18-
<pre>
19-
<strong>Input:</strong>
20-
5
21-
/ \
22-
1 4
23-
&nbsp; / \
24-
&nbsp; 3 6
25-
<strong>Output:</strong> false
26-
<strong>Explanation:</strong> Input: [5,1,4,null,null,3,6].
27-
&nbsp; the value of root node is 5, but its right child has value 4.</pre>
28-
29-
30-
31-
## Solutions
32-
33-
34-
### Python3
35-
36-
```python
37-
38-
```
39-
40-
### Java
41-
42-
```java
43-
44-
```
45-
46-
### ...
47-
```
48-
49-
```
1+
# [04.05. Legal Binary Search Tree](https://leetcode-cn.com/problems/legal-binary-search-tree-lcci)
2+
3+
## Description
4+
<p>Implement a function to check if a binary tree is a binary search tree.</p>
5+
6+
7+
8+
<p><strong>Example&nbsp;1:</strong></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong>Input:</strong>
15+
16+
2
17+
18+
/ \
19+
20+
1 3
21+
22+
<strong>Output:</strong> true
23+
24+
</pre>
25+
26+
27+
28+
<p><strong>Example&nbsp;2:</strong></p>
29+
30+
31+
32+
<pre>
33+
34+
<strong>Input:</strong>
35+
36+
5
37+
38+
/ \
39+
40+
1 4
41+
42+
&nbsp; / \
43+
44+
&nbsp; 3 6
45+
46+
<strong>Output:</strong> false
47+
48+
<strong>Explanation:</strong> Input: [5,1,4,null,null,3,6].
49+
50+
&nbsp; the value of root node is 5, but its right child has value 4.</pre>
51+
52+
53+
54+
55+
## Solutions
56+
57+
58+
### Python3
59+
60+
```python
61+
# Definition for a binary tree node.
62+
# class TreeNode:
63+
# def __init__(self, x):
64+
# self.val = x
65+
# self.left = None
66+
# self.right = None
67+
68+
class Solution:
69+
res, t = True, None
70+
def isValidBST(self, root: TreeNode) -> bool:
71+
self.isValid(root)
72+
return self.res
73+
74+
def isValid(self, root):
75+
if not root:
76+
return
77+
self.isValid(root.left)
78+
if self.t is None or self.t < root.val:
79+
self.t = root.val
80+
else:
81+
self.res = False
82+
return
83+
self.isValid(root.right)
84+
```
85+
86+
### Java
87+
88+
```java
89+
/**
90+
* Definition for a binary tree node.
91+
* public class TreeNode {
92+
* int val;
93+
* TreeNode left;
94+
* TreeNode right;
95+
* TreeNode(int x) { val = x; }
96+
* }
97+
*/
98+
class Solution {
99+
private boolean res = true;
100+
private Integer t = null;
101+
public boolean isValidBST(TreeNode root) {
102+
isValid(root);
103+
return res;
104+
}
105+
106+
private void isValid(TreeNode root) {
107+
if (root == null) {
108+
return;
109+
}
110+
isValid(root.left);
111+
if (t == null || t < root.val) {
112+
t = root.val;
113+
} else {
114+
res = false;
115+
return;
116+
}
117+
isValid(root.right);
118+
}
119+
}
120+
```
121+
122+
### ...
123+
```
124+
125+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
private boolean res = true;
12+
private Integer t = null;
13+
public boolean isValidBST(TreeNode root) {
14+
isValid(root);
15+
return res;
16+
}
17+
18+
private void isValid(TreeNode root) {
19+
if (root == null) {
20+
return;
21+
}
22+
isValid(root.left);
23+
if (t == null || t < root.val) {
24+
t = root.val;
25+
} else {
26+
res = false;
27+
return;
28+
}
29+
isValid(root.right);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
res, t = True, None
10+
def isValidBST(self, root: TreeNode) -> bool:
11+
self.isValid(root)
12+
return self.res
13+
14+
def isValid(self, root):
15+
if not root:
16+
return
17+
self.isValid(root.left)
18+
if self.t is None or self.t < root.val:
19+
self.t = root.val
20+
else:
21+
self.res = False
22+
return
23+
self.isValid(root.right)

0 commit comments

Comments
 (0)