Skip to content

Commit bff2ec6

Browse files
committed
Add example
1 parent fdb993c commit bff2ec6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

interactiveExamples/counting_trees.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Given an int n, return the number of possible trees with n nodes you can construct
2+
# Example: n = 1 --> 1, the single node
3+
# n = 2 --> 2, a parent node with a left child and a parent node with a right child
4+
5+
def solution(n):
6+
pass

interactiveExamples/tree_to_linked_list.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,35 @@ def __init__(self, val, left=None, right=None):
88
self.left = left
99
self.right = right
1010

11+
def __str__(self):
12+
return self.val
13+
1114
def solution(root):
12-
pass
15+
if root is None:
16+
return root
17+
if root.left is None and root.right is None:
18+
return root
19+
20+
stack = []
21+
stack.append(root)
22+
23+
head = root
24+
tail = None
25+
26+
while len(stack) > 0:
27+
curr = stack.pop()
28+
29+
if curr.left is not None:
30+
stack.append(curr.left)
31+
if curr.right is not None:
32+
stack.append(curr.right)
33+
34+
if tail is None:
35+
tail = curr
36+
tail.left = None
37+
else:
38+
tail.right = curr
39+
curr.left = tail
40+
tail = curr
41+
tail.right = None
42+
return head

0 commit comments

Comments
 (0)