Skip to content

Commit c06cd86

Browse files
committed
'248-250'
1 parent 5e6da95 commit c06cd86

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,9 @@ $=link=$: 题目原题链接
549549

550550
247.[盛最多水的容器](https://leetcode.cn/problems/container-with-most-water/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/ContainerWithMostWater/ContainerWithMostWater.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/ContainerWithMostWater/ContainerWithMostWater.py)):题目难度(中等),题目类型(贪心_数组_双指针)
551551

552+
248.[在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/FindLargestValueInEachTreeRow/FindLargestValueInEachTreeRow.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/FindLargestValueInEachTreeRow/FindLargestValueInEachTreeRow.py)):题目难度(中等),题目类型(树_深度优先搜索_广度优先搜索_二叉树)
553+
554+
249.[删除二叉搜索树中的节点](https://leetcode.cn/problems/delete-node-in-a-bst/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/DeleteNodeInABst/DeleteNodeInABst.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/DeleteNodeInABst/DeleteNodeInABst.py)):题目难度(中等),题目类型(树_二叉搜索树_二叉树)
555+
556+
250.[验证回文串](https://leetcode.cn/problems/valid-palindrome/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/ValidPalindrome/ValidPalindrome.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/ValidPalindrome/ValidPalindrome.py)):题目难度(简单),题目类型(双指针_字符串)
557+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.dbc.code;
2+
3+
public class DeleteNodeInABst {
4+
public class TreeNode {
5+
int val;
6+
TreeNode left;
7+
TreeNode right;
8+
9+
TreeNode() {
10+
}
11+
12+
TreeNode(int val) {
13+
this.val = val;
14+
}
15+
16+
TreeNode(int val, TreeNode left, TreeNode right) {
17+
this.val = val;
18+
this.left = left;
19+
this.right = right;
20+
}
21+
}
22+
23+
public TreeNode deleteNode(TreeNode root, int key) {
24+
if (root == null) return null;
25+
else if (root.val < key) root.right = deleteNode(root.right, key);
26+
else if (root.val > key) root.left = deleteNode(root.left, key);
27+
else if (root.left == null || root.right == null) root = root.left != null ? root.left : root.right;
28+
else {
29+
TreeNode node = root.right;
30+
while (node.left != null) {
31+
node = node.left;
32+
}
33+
node.left = root.left;
34+
return root.right;
35+
}
36+
return root;
37+
}
38+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Optional
2+
3+
class TreeNode:
4+
def __init__(self, val=0, left=None, right=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
9+
def deleteNode(root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
10+
if root is None:
11+
return None
12+
elif root.val > key:
13+
root.left = deleteNode(root.left, key)
14+
elif root.val < key:
15+
root.right = deleteNode(root.right, key)
16+
elif root.left is None or root.right is None:
17+
root = root.left if root.left else root.right
18+
else:
19+
node = root.right
20+
while node.left:
21+
node = node.left
22+
node.left = root.left
23+
return root.right
24+
return root
25+
26+
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.dbc.code;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
public class FindLargestValueInEachTreeRow {
9+
public class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
TreeNode() {
15+
}
16+
17+
TreeNode(int val) {
18+
this.val = val;
19+
}
20+
21+
TreeNode(int val, TreeNode left, TreeNode right) {
22+
this.val = val;
23+
this.left = left;
24+
this.right = right;
25+
}
26+
}
27+
28+
public List<Integer> largestValues(TreeNode root) {
29+
if (root == null) return new ArrayList<>();
30+
Deque<TreeNode> queue = new ArrayDeque<>();
31+
queue.add(root);
32+
List<Integer> res = new ArrayList<>();
33+
while (!queue.isEmpty()) {
34+
int length = queue.size();
35+
int max = Integer.MIN_VALUE;
36+
for (int i = 0; i < length; i++) {
37+
TreeNode node = queue.pollFirst();
38+
max = Math.max(max, node.val);
39+
if (node.left != null) queue.add(node.left);
40+
if (node.right != null) queue.add(node.right);
41+
}
42+
res.add(max);
43+
}
44+
return res;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Optional, List
2+
from collections import deque
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
def largestValues(root: Optional[TreeNode]) -> List[int]:
11+
if not root:
12+
return []
13+
queue = deque([root])
14+
res = []
15+
while queue:
16+
length, layer_max = len(queue), -2 ** 31
17+
for i in range(length):
18+
node = queue.popleft()
19+
if node.left:
20+
queue.append(node.left)
21+
if node.right:
22+
queue.append(node.right)
23+
layer_max = max(layer_max, node.val)
24+
res.append(layer_max)
25+
26+
return res
27+
28+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.dbc.code;
2+
3+
import java.util.Locale;
4+
5+
public class ValidPalindrome {
6+
public boolean isPalindrome(String s) {
7+
int left = 0, right = s.length() - 1;
8+
s = s.toLowerCase();
9+
while (left < right) {
10+
while (left < right && !Character.isDigit(s.charAt(left)) && !Character.isLetter(s.charAt(left))) left++;
11+
while (left < right && !Character.isDigit(s.charAt(right)) && !Character.isLetter(s.charAt(right))) right--;
12+
if (left < right && s.charAt(left) != s.charAt(right)) {
13+
return false;
14+
}
15+
left++;
16+
right--;
17+
}
18+
return true;
19+
}
20+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def isPalindrome(s: str) -> bool:
2+
left, right = 0, len(s) - 1
3+
s = s.lower()
4+
while left < right:
5+
while left < right and not s[left].isdigit() and not s[left].isalpha():
6+
left += 1
7+
while left < right and not s[right].isdigit() and not s[right].isalpha():
8+
right -= 1
9+
if left < right and s[left] != s[right]:
10+
return False
11+
left += 1
12+
right -= 1
13+
return True
14+
15+
16+

0 commit comments

Comments
 (0)