Skip to content

Commit

Permalink
fixing inorder traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
k-schmidt committed Apr 22, 2017
1 parent 764269a commit 629d35a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tree/traversal/inorder.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
class Node:

def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right


def inorder(root):
res = []
if not root:
return res
stack = []
while root and stack:
while root or stack:
while root:
stack.append(root)
root = root.left
root = stack.pop()
res.add(root.val)
res.append(root.val)
root = root.right
return res

if __name__ == '__main__':
n1 = Node(100)
n2 = Node(50)
n3 = Node(150)
n4 = Node(25)
n5 = Node(75)
n6 = Node(125)
n7 = Node(175)
n1.left, n1.right = n2, n3
n2.left, n2.right = n4, n5
n3.left, n3.right = n6, n7
print(inorder(n1))

0 comments on commit 629d35a

Please sign in to comment.