Question

[Python]

Construct Tree Using Inorder and Preorder

Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root.

Note: Assume binary tree contains only unique elements.

Input format :

Line 1 : n (Total number of nodes in binary tree)

Line 2 : Pre order traversal

Line 3 : Inorder Traversal

Output Format :

Elements are printed level wise, each level in new line (separated by space).

Sample Input :

12
1 2 3 4 15 5 6 7 8 10 9 12
4 15 3 2 5 1 6 10 8 7 9 12

Sample Output :

1 
2 6 
3 5 7 
4 8 9 
15 10 12

Solution:

import queue
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

def buildTreePreOrder(preorder, inorder):
# Given Preorder and Inorder traversal of a binary tree, create the binary
# tree associated with the traversals.You just need to construct the tree
# and return the root. For 12 Nodes with following input:
# preOrder: 1 2 3 4 15 5 6 7 8 10 9 12
# inOrder: 4 15 3 2 5 1 6 10 8 7 9 12
#############################
# PLEASE ADD YOUR CODE HERE #
#############################
pass

def printLevelATNewLine(root):
# Given a binary tree, print the level order traversal. Make sure each level
# start in new line.
if root==None:
return
inputQ = queue.Queue()
outputQ = queue.Queue()
inputQ.put(root)
while not inputQ.empty():
while not inputQ.empty():
curr = inputQ.get()
print(curr.data, end=' ')
if curr.left!=None:
outputQ.put(curr.left)
if curr.right!=None:
outputQ.put(curr.right)
print()
inputQ, outputQ = outputQ, inputQ

# Main
n=int(input())
preorder = [int(i) for i in input().strip().split()]
inorder = [int(i) for i in input().strip().split()]
root = buildTreePreOrder(preorder, inorder)
printLevelATNewLine(root)

Code looks like this in editor:

In [ ]: import queue class Binary TreeNode: definit__(self, data): self.data = data self.left = None self.right = None def bu

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def ConstructTree(inOrder, preOrder, inStrt, inEnd):

     if (inStrt > inEnd) :

        return None

CurrentNode = Node(preOrder[ConstructTree.preIndex])

ConstructTree.preIndex += 1

    if inStrt == inEnd :

        return CurrentNode

    inIndex = search(inOrder, inStrt, inEnd, tNode.data)

CurrentNode.left = ConstructTree(inOrder, preOrder, inStrt, inIndex-1)

CurrentNode.right = ConstructTree(inOrder, preOrder, inIndex + 1, inEnd)

  return currentNode

Hope this code helps you. For better Understanding try to dry run. Please give an upvote if it does.

  

Add a comment
Know the answer?
Add Answer to:
[Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT