Skip to content

Commit 711e987

Browse files
committed
it ain't much, but it's honest work :(
1 parent 2520282 commit 711e987

3 files changed

+81
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 958. Check Completeness of a Binary Tree
2+
# https://leetcode.com/problems/check-completeness-of-a-binary-tree/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode:
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution:
12+
def isCompleteTree(self, root: TreeNode) -> bool:
13+
q = [root]
14+
while q and q[0] is not None:
15+
x = q.pop(0)
16+
q.append(x.left)
17+
q.append(x.right)
18+
while q:
19+
x = q.pop(0)
20+
if x is not None:
21+
return False
22+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 340. Longest Substring with At Most K Distinct Characters
2+
# https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
3+
4+
class Solution:
5+
def lengthOfLongestSubstringKDistinct(self, s: str, k: int) -> int:
6+
from collections import defaultdict
7+
8+
w = defaultdict(int)
9+
i, j, n, ans = 0, 0, len(s), 0
10+
11+
while j < n:
12+
w[s[j]] += 1
13+
while len(w) > k:
14+
c = s[i]
15+
if c in w:
16+
w[c] -= 1
17+
if w[c] == 0:
18+
del w[c]
19+
i += 1
20+
ans = max(ans, j-i+1)
21+
j += 1
22+
23+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 865. Smallest Subtree with all the Deepest Nodes
2+
# https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode:
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution:
12+
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
13+
deepest = (root, 0,)
14+
d = dict()
15+
s = [(root, 0,)]
16+
while s:
17+
x_node, x_depth = s.pop()
18+
d[x_node.val] = x_depth
19+
if x_depth > deepest[1]:
20+
deepest = (x_node, x_depth,)
21+
if x_node.left:
22+
s.append((x_node.left, x_depth+1,))
23+
if x_node.right:
24+
s.append((x_node.right, x_depth+1,))
25+
26+
def ans(node):
27+
if node is None or d[node.val] == deepest[1]:
28+
return node
29+
l, r = ans(node.left), ans(node.right)
30+
if l and r:
31+
return node
32+
elif l and not r:
33+
return l
34+
return r
35+
36+
return ans(root)

0 commit comments

Comments
 (0)