Skip to content

Commit b28de27

Browse files
author
Mauricio Klein
committed
Add solution for challenge #61
1 parent ead9a9e commit b28de27

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@
6060
- [x] [Challenge 58: Get all Values at a Certain Height in a Binary Tree](challenge-58/)
6161
- [x] [Challenge 59: Sort Colors](challenge-59/)
6262
- [x] [Challenge 60: Group anagrams](challenge-60/)
63+
- [x] [Challenge 61: Full Binary Tree](challenge-61/)

challenge-61/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
python test_*.py
3+
4+
.PHONY: test

challenge-61/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Full Binary Tree
2+
3+
This problem was asked by Google.
4+
5+
## Description
6+
7+
Given a binary tree, remove the nodes in which there is only 1 child, so that the binary tree is a full binary tree.
8+
9+
So leaf nodes with no children should be kept, and nodes with 2 children should be kept as well.
10+
11+
## Example
12+
13+
```
14+
Given this tree:
15+
1
16+
/ \
17+
2 3
18+
/ / \
19+
0 9 4
20+
21+
We want a tree like:
22+
1
23+
/ \
24+
0 3
25+
/ \
26+
9 4
27+
```

challenge-61/__init__.py

Whitespace-only changes.

challenge-61/solver.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections import deque
2+
3+
class Node(object):
4+
def __init__(self, value, left=None, right=None):
5+
self.value = value
6+
self.left = left
7+
self.right = right
8+
9+
def __str__(self):
10+
q = deque()
11+
q.append(self)
12+
result = ''
13+
while len(q):
14+
num = len(q)
15+
while num > 0:
16+
n = q.popleft()
17+
result += str(n.value)
18+
if n.left:
19+
q.append(n.left)
20+
if n.right:
21+
q.append(n.right)
22+
num = num - 1
23+
if len(q):
24+
result += "\n"
25+
26+
return result
27+
28+
def full_binary_tree(node):
29+
if not node:
30+
return None
31+
32+
left = full_binary_tree(node.left)
33+
right = full_binary_tree(node.right)
34+
35+
if (left and right) or (not left and not right):
36+
node.left = left
37+
node.right = right
38+
return node
39+
else:
40+
return left or right

challenge-61/test_solver.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
from solver import Node, full_binary_tree
3+
4+
class TestSolver(unittest.TestCase):
5+
def test_full_binary_tree(self):
6+
# 1 1
7+
# / \ / \
8+
# 2 3 ----> 0 3
9+
# / / \ / \
10+
# 0 9 4 9 4
11+
root = Node(1, Node(2, Node(0)), Node(3, Node(9), Node(4)))
12+
expected_str_output = "1\n03\n94"
13+
14+
self.assertEqual(str(full_binary_tree(root)), expected_str_output)
15+
16+
# 1 1
17+
# / \ / \
18+
# 3 2 ----> 3 4
19+
# / \ \ / \
20+
# 0 9 4 0 9
21+
root = Node(1, Node(3, Node(0), Node(9)), Node(2, right=Node(4)))
22+
expected_str_output = "1\n34\n09"
23+
24+
self.assertEqual(str(full_binary_tree(root)), expected_str_output)
25+
26+
# 1 3
27+
# / ---->
28+
# 3
29+
root = Node(1, Node(3))
30+
expected_str_output = "3"
31+
32+
self.assertEqual(str(full_binary_tree(root)), expected_str_output)
33+
34+
# 1 3
35+
# \ ---->
36+
# 3
37+
root = Node(1, right=Node(3))
38+
expected_str_output = "3"
39+
40+
self.assertEqual(str(full_binary_tree(root)), expected_str_output)
41+
42+
if __name__ == "__main__":
43+
unittest.main()

0 commit comments

Comments
 (0)