Skip to content

Commit 0405c69

Browse files
committed
Add lessons
Add lessons #1 #2 and #3
1 parent f9d3288 commit 0405c69

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Daily Coding Problem
2-
A repository of my own solutions as well as optimal solutions for [Daily Coding Problem](https://www.dailycodingproblem.com/).
2+
A repository of my solutions for [Daily Coding Problem](https://www.dailycodingproblem.com/).
33

4-
[Index](./index.md) contains the list of programs.
4+
[Index](./index.md) contains the list Daily Coding Problems and lessons.
55

6-
The programs are located in the [solutions](./solutions) folder.
6+
Daily Coding Problem solutions are in [challenges](./challenges).
7+
8+
Lessons and guides by DCP are in [lessons](./lessons).

index.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
6. [XOR Linked List](./challenges/xor-linked-list.py)
88
7. [Decode Ways](./challenges/decode-ways.py)
99
8. [Count Unival Subtrees](./challenges/count-unival-subtrees.py)
10+
11+
# Lessons
12+
1. [Merge Sort](./lessons/merge-sort.py)
13+
2. [Count Nodes in Binary Tree](./lessons/count-nodes-in-binary-tree.py)
14+
3. [Deepest Node in Binary Tree](./lessons/deepest-node-in-binary-tree.py)

lessons/count-nodes-in-binary-tree.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Given the root to a binary tree, count the total number of nodes there are.
3+
4+
"""
5+
6+
class Node:
7+
def __init__(self, val, left=None, right=None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
def count(node):
13+
return count(node.left) + count(node.right) + 1 if node else 0
14+
15+
if __name__ == "__main__":
16+
tree = Node(4, Node(2, Node(1), Node(3)), Node(5))
17+
"""
18+
4
19+
/ \
20+
2 5
21+
/ \
22+
1 3
23+
24+
"""
25+
print(count(tree))
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Given the root to a binary tree, return the deepest node.
3+
4+
"""
5+
6+
class Node:
7+
def __init__(self, val, left=None, right=None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
def __str__(self):
13+
return "<" + str(self.val) + ">"
14+
15+
def deepest(node):
16+
if node and not node.left and not node.right:
17+
return (node, 1) # Leaf and its depth
18+
19+
if not node.left: # Then the deepest node is on the right subtree
20+
return increment_depth(deepest(node.right))
21+
elif not node.right: # Then the deepest node is on the left subtree
22+
return increment_depth(deepest(node.left))
23+
24+
return increment_depth(
25+
max(deepest(node.left), deepest(node.right),
26+
key=lambda x: x[1]))
27+
28+
def increment_depth(node_depth_tuple):
29+
node, depth = node_depth_tuple
30+
return (node, depth + 1)
31+
32+
if __name__ == "__main__":
33+
tree = Node(4, Node(2, Node(1, Node(0)), Node(3)), Node(5))
34+
"""
35+
4
36+
/ \
37+
2 5
38+
/ \
39+
1 3
40+
/
41+
0
42+
"""
43+
node_depth_tuple = deepest(tree)
44+
print(node_depth_tuple[0], " of depth ", node_depth_tuple[1])

lessons/merge-sort.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Return a new sorted merged list from K sorted lists, each with size N.
3+
4+
"""
5+
6+
import heapq
7+
8+
def merge(lists):
9+
merged_list = []
10+
11+
heap = [(lst[0], i, 0) for i, lst in enumerate(lists) if lst]
12+
heapq.heapify(heap)
13+
14+
while heap:
15+
val, list_ind, element_ind = heapq.heappop(heap)
16+
17+
merged_list.append(val)
18+
19+
if element_ind + 1 < len(lists[list_ind]):
20+
next_tuple = (lists[list_ind][element_ind + 1],
21+
list_ind,
22+
element_ind + 1)
23+
heapq.heappush(heap, next_tuple)
24+
return merged_list
25+
26+
if __name__ == "__main__":
27+
print(merge([[1], [1, 3, 5], [1, 10, 20, 30, 40]]))

0 commit comments

Comments
 (0)