Skip to content

Commit 54e08d7

Browse files
author
Mauricio Klein
committed
Add solution for challenge #42
1 parent d782511 commit 54e08d7

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
4141
- [x] [Challenge 39: Remove Consecutive Nodes that Sum to 0](challenge-39/)
4242
- [x] [Challenge 40: Queue Using Two Stacks](challenge-40/)
4343
- [x] [Challenge 41: Maximum sequence of consecutive numbers](challenge-41/)
44+
- [x] [Challenge 42: Depth of a stringified binary tree](challenge-42/)

challenge-42/Makefile

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

challenge-42/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Depth of a stringified binary tree
2+
3+
This problem was asked by Linkedin.
4+
5+
## Description
6+
7+
You are given a binary tree in a peculiar string representation. Each node is written in the form (lr), where l corresponds to the left child and r corresponds to the right child.
8+
9+
If either l or r is null, it will be represented as a zero. Otherwise, it will be represented by a new (lr) pair.
10+
11+
Given this representation, determine the depth of the tree.
12+
13+
## Example
14+
```
15+
Input: ((00)(00))
16+
Output: 2
17+
```

challenge-42/__init__.py

Whitespace-only changes.

challenge-42/solver.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Node(object):
2+
def __init__(self):
3+
self.left = self.right = None
4+
5+
def __str__(self):
6+
left = '0' if self.left == None else str(self.left )
7+
right = '0' if self.right == None else str(self.right)
8+
9+
return "({}{})".format(left, right)
10+
11+
#
12+
# Time complexity: O(n+m) (where "n" is the string length and "m" is the number of nodes in the tree)
13+
# Space complexity: O(m) (for the tree representation)
14+
#
15+
def tree_depth(stringified_tree):
16+
# 1. Transform string into a tree
17+
stack = []
18+
19+
for char in stringified_tree:
20+
if char == '(':
21+
stack.append(Node())
22+
elif char == '0':
23+
stack.append(None)
24+
elif char == ')':
25+
right, left, root = stack.pop(), stack.pop(), stack.pop()
26+
27+
root.left = left
28+
root.right = right
29+
30+
stack.append(root)
31+
32+
# 2. Calculate the depth from the root
33+
root = stack.pop()
34+
return depth(root)
35+
36+
def depth(node):
37+
if not node:
38+
return 0
39+
40+
return 1 + max(depth(node.left), depth(node.right))

challenge-42/test_solver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from solver import tree_depth
3+
4+
class TestSolver(unittest.TestCase):
5+
def test_tree_depth(self):
6+
self.assertEqual(tree_depth("(00)" ), 1)
7+
self.assertEqual(tree_depth("((00)(00))" ), 2)
8+
self.assertEqual(tree_depth("((0(00))(00))"), 3)
9+
10+
if __name__ == "__main__":
11+
unittest.main()

0 commit comments

Comments
 (0)