-
Notifications
You must be signed in to change notification settings - Fork 7
/
Compare Leaf Traversal.py
128 lines (98 loc) · 4.14 KB
/
Compare Leaf Traversal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Compare Leaf Traversal
Write a function that takes in the root nodes of two Binary Trees and returns a boolean representing whether their
leaf traversals are the same.
The leaf traversal of a Binary Tree traverses only its leaf nodes from left to right. A leaf node is any node that
has no left or right children.
For example, the leaf traversal of the following Binary Tree is 1, 3, 2.
tree = 4
/ \
1 5
/ \
3 2
Each BinaryTree node has an integer value, a left child node, and a right child node. Children nodes can either be BinaryTree nodes
themselves or None / null.
Sample Input
tree1 = 1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
tree2 = 1
/ \
2 3
/ \ \
4 7 5
/ \
8 6
Sample Output
true
"""
# SOLUTION 1
# This is an input class. Do not edit.
class BinaryTree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# O(n + m) time | (h1 + h2) space where n is the number of nodes in the first
# Binary Tree, m is the number in the second, hi is the height of the first Binary
# Tree, and h2 is the height of the second
def compareLeafTraversal(tree1, tree2):
tree1TraversalStack = [tree1]
tree2TraversalStack = [tree2]
while len(tree1TraversalStack) > 0 and len(tree2TraversalStack) > 0:
tree1Leaf = getNextLeafNode(tree1TraversalStack)
tree2Leaf = getNextLeafNode(tree2TraversalStack)
if tree1Leaf.value != tree2Leaf.value:
return False
return len(tree1TraversalStack) == 0 and len(tree2TraversalStack) == 0
def getNextLeafNode(traversalStack):
currentNode = traversalStack.pop()
while not isLeafNode(currentNode):
if currentNode.right is not None:
traversalStack.append(currentNode.right)
# We purposely add the left node to the stack after the
# right node so that it gets popped off the stack first.
if currentNode.left is not None:
traversalStack.append(currentNode.left)
currentNode = traversalStack.pop()
return currentNode
def isLeafNode(node):
return node.left is None and node.right is None
# SOLUTION 2
# This is an input class. Do not edit.
class BinaryTree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# O(n + m) time | 0(max(h1, h2)) space where n is the number of nodes in the first
# Binary Tree, m is the number in the second, hi is the height of the first Binary
# Tree, and h2 is the height of the second
def compareLeafTraversal(tree1, tree2):
treelLeafNodesLinkedList, _ = connectLeafNodes(tree1)
tree2LeafNodesLinkedList, _ = connectLeafNodes(tree2)
list1CurrentNode = treelLeafNodesLinkedList
list2CurrentNode = tree2LeafNodesLinkedList
while list1CurrentNode is not None and list2CurrentNode is not None:
if list1CurrentNode.value != list2CurrentNode.value:
return False
list1CurrentNode = list1CurrentNode.right
list2CurrentNode = list2CurrentNode.right
return list1CurrentNode is None and list2CurrentNode is None
def connectLeafNodes(currentNode, head=None, previousNode=None):
if currentNode is None:
return head, previousNode
if isLeafNode(currentNode):
if previousNode is None:
head = currentNode
else:
previousNode.right = currentNode
previousNode = currentNode
leftHead, leftPreviousNode = connectLeafNodes(currentNode.left, head, previousNode)
return connectLeafNodes(currentNode.right, leftHead, leftPreviousNode)
def isLeafNode(node):
return node.left is None and node.right is None