Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BJ001-1904038 #312

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Week_02/id_38/LeetCode_671_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from collections import deque


class Solution:
def findSecondMinimumValue(self, root):
if not root:
return -1
if not root.left or not root.right:
return -1
minimum_value = None
queue = deque()
queue.append(root)
while queue:
for _ in range(len(queue)):
node = queue.popleft()
if node.left:
if not minimum_value:
queue.append(node.left)
elif node.left.val <= root.val + 1:
queue.append(node.left)
if node.right:
if not minimum_value:
queue.append(node.right)
if node.right.val <= root.val + 1:
queue.append(node.right)
if node.val - 1 == root.val:
return node.val
if not minimum_value and root.val < node.val:
minimum_value = node.val
elif minimum_value and node.val != root.val:
minimum_value = min(minimum_value, node.val)
if not minimum_value:
return -1
return minimum_value
38 changes: 38 additions & 0 deletions Week_02/id_38/LeetCode_783_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from collections import deque


class Solution:
def minDiffInBST(self, root):
if not root:
return None
if not root.left and not root.right:
return None
stack = deque()
visited = []
stack.append(root)
while stack:
node = stack.pop()
if node in visited:
continue
if node.right and node.right not in visited:
stack.append(node.right)
if not node.left or node.left in visited:
visited.append(node)
else:
stack.append(node)
if node.left and node.left not in visited:
stack.append(node.left)
last_node = None
minimum_value = None
while visited:
current_node = visited.pop()
if last_node and last_node.val + 1 == current_node.val:
return 1
if last_node:
space_value = abs(current_node.val - last_node.val)
else:
space_value = current_node.val
last_node = current_node
if not minimum_value or (minimum_value and minimum_value > space_value):
minimum_value = space_value
return minimum_value
58 changes: 57 additions & 1 deletion Week_02/id_38/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# 学习笔记
# 学习笔记
## 二叉查找树(Binary Search Tree)
二叉查找树有以下特点:
* 根节点的值大于左子树的值,小于等于右子树的值。
* 使用二叉树中序遍历的方式可以得到一个有序的数据集。
*
## 二叉树(Binary Tree)

二叉树的遍历:前序遍历,中序遍历,后序遍历,以及广度优先遍历(DFS)和深度优先遍历(BFS)。

前序遍历,中序遍历,后序遍历的实现使用递归的方式很简单。如果用递归可以实现,那么也可以使用迭代的方式实现。

以下代码是用栈实现二叉树非递归的中序遍历。

```python
def in_order(self, root):
stack = deque()
visited = []
stack.append(root)
while stack:
node = stack.pop()
if node in visited:
continue
if node.right and node.right not in visited:
stack.append(node.right)
if not node.left or node.left in visited:
visited.append(node)
else:
stack.append(node)
if node.left and node.left not in visited:
stack.append(node.left)

for item in visited:
print(item.val)
```

二叉树的按层遍历(DFS)算法代码实现如下:

```python
def dfs(self, root):
if not root:
return
depth = 1
queue = deque()
queue.append(root)
while queue:
for _ in range(len(queue)):
node = queue.popleft()
print(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
print("*******************")
depth += 1
```

118 changes: 118 additions & 0 deletions Week_03/id_38/LeetCode_104_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1


class Solution:

def maxDepth(self, root):
if not root:
return 0
return self.get_tree_max_depth(root, 0)

def get_tree_max_depth(self, root, depth):
if not root:
return depth
return max(self.get_tree_max_depth(root.left, depth), self.get_tree_max_depth(root.right, depth)) + 1



21 changes: 21 additions & 0 deletions Week_03/id_38/LeetCode_429_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from collections import deque


class Solution:
def levelOrder(self, root):
result = []
if not root:
return result
queue = deque()
queue.append(root)
while queue:
items = []
for _ in range(len(queue)):
node = queue.popleft()
items.append(node.val)
for item in node.children:
queue.append(item)
result.append(items)
return result


19 changes: 19 additions & 0 deletions Week_03/id_38/LeetCode_703_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import heapq


class KthLargest:
def __init__(self, k, nums):
self.nums = nums
self.k = k
heapq.heapify(self.nums)
while len(self.nums) > k:
heapq.heappop(self.nums)

def add(self, val):
if len(self.nums) < self.k:
heapq.heappush(self.nums, val)
elif val > self.nums[0]:
heapq.heapreplace(self.nums, val)
return self.nums[0]


25 changes: 25 additions & 0 deletions Week_03/id_38/LeetCode_997_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def findJudge(self, n, trusts):
if n == 1 and len(trusts) == 0:
return 1
followers = [None] * (n + 1)
followings = [None] * (n + 1)
for trust in trusts:
person = trust[0]
following = trust[1]
if not followings[person]:
followings[person] = []
followings[person].append(following)
if not followers[following]:
followers[following] = []
followers[following].append(person)
for i in range(1, n + 1):
follower = followers[i]
following = followings[i]
if following or not follower:
continue
if len(follower) == n - 1:
return i
return -1


6 changes: 6 additions & 0 deletions Week_04/id_38/LeetCode_169_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def majorityElement(self, nums):
nums = sorted(nums)
return nums[len(nums) // 2]


29 changes: 29 additions & 0 deletions Week_04/id_38/LeetCode_702_38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class TrieNode:
def __init__(self, data):
self.data = data
self.isEnding = False
self.children = [None] * 26

class Solution:
def longestWord(self, words):
words = sorted(words)
longest_word = None
root = TrieNode("/")
for i in range(len(words)):
word = words[i]
if not longest_word:
longest_word = word
p = root
length_of_word = len(word)
word_is_valid = True
for j in range(length_of_word):
index = ord(word[j]) - ord("a")
if not p.children[index] and j == length_of_word - 1:
p.children[index] = TrieNode(word[j])
elif not p.children[index]:
word_is_valid = False
break
p = p.children[index]
if len(word) > len(longest_word) and word > longest_word and word_is_valid:
longest_word = word
return longest_word