Iterative Preorder Traversal

70 篇文章 0 订阅

Iterative Preorder Traversal

Given a Binary Tree, write an iterative function to print Preorder traversal of the given binary tree.

Refer this for recursive preorder traversal of Binary Tree. To convert an inherently recursive procedures to iterative, we need an explicit stack. Following is a simple stack based iterative process to print Preorder traversal.

1) Create an empty stack nodeStack and push root node to stack.
2) Do following while nodeStack is not empty.
….a) Pop an item from stack and print it.
….b) Push right child of popped item to stack
….c) Push left child of popped item to stack

Right child is pushed before left child to make sure that left subtree is processed first.

# Python program to perform iterative preorder traversal 

# A binary tree node 
class Node: 

	# Constructor to create a new node 
	def __init__(self, data): 
		self.data = data 
		self.left = None
		self.right = None

# An iterative process to print preorder traveral of BT 
def iterativePreorder(root): 
	
	# Base CAse 
	if root is None: 
		return

	# create an empty stack and push root to it 
	nodeStack = [] 
	nodeStack.append(root) 

	# Pop all items one by one. Do following for every popped item 
	# a) print it 
	# b) push its right child 
	# c) push its left child 
	# Note that right child is pushed first so that left 
	# is processed first */ 
	while(len(nodeStack) > 0): 
		
		# Pop the top item from stack and print it 
		node = nodeStack.pop() 
		print node.data, 
		
		# Push right and left children of the popped node 
		# to stack 
		if node.right is not None: 
			nodeStack.append(node.right) 
		if node.left is not None: 
			nodeStack.append(node.left) 
	
# Driver program to test above function 
root = Node(10) 
root.left = Node(8) 
root.right = Node(2) 
root.left.left = Node(3) 
root.left.right = Node(5) 
root.right.left = Node(2) 
iterativePreorder(root) 

# This code is contributed by Nikhil Kumar Singh(nickzuck_007) 

References:

https://www.geeksforgeeks.org/iterative-preorder-traversal/

My codes

class Solution1(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root is None:
            return []

        stack, output = [root, ], []
        while stack:
            root = stack.pop()
            output.append(root.item)
            if root.left is not None:
                stack.append(root.left)
            if root.right is not None:
                stack.append(root.right)
        print output      
        return output[::-1]
    
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root is None:
            return []

        stack, output = [root, ], []
        while stack:
            root = stack.pop()
            output.append(root.item)
            if root.right is not None:
                stack.append(root.right)

            if root.left is not None:
                stack.append(root.left) 
        return output
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值