Tag: LIFO

Inorder Tree Traversal | Iterative & Recursive

Given a binary tree, write iterative and recursive solution to traverse the tree using in-order traversal.   Unlike linked lists, one-dimensional arrays and other linear data structures, which are traversed in linear order, trees may be traversed in multiple ways in depth-first order (in-order, pre-order and post-order) or breadth-first order (level order traversal). Beyond these …

Read More Inorder Tree Traversal | Iterative & Recursive

Preorder Tree Traversal | Iterative & Recursive

Given a binary tree, write iterative and recursive solution to traverse the tree using pre-order traversal.   Unlike linked lists, one-dimensional arrays and other linear data structures, which are traversed in linear order, trees may be traversed in multiple ways in depth-first order (pre-order, pre-order and pre-order) or breadth-first order (level order traversal). Beyond these …

Read More Preorder Tree Traversal | Iterative & Recursive

Postorder Tree Traversal | Iterative & Recursive

Given a binary tree, write iterative and recursive solution to traverse the tree using post-order traversal.   Unlike linked lists, one-dimensional arrays and other linear data structures, which are traversed in linear order, trees may be traversed in multiple ways in depth-first order (post-order, pre-order and post-order) or breadth-first order (level order traversal). Beyond these …

Read More Postorder Tree Traversal | Iterative & Recursive

Check if two binary trees are identical or not | Iterative & Recursive

Write an efficient algorithm to check if two binary trees are identical or not. i.e. if they have identical structure & their contents are also same.     The idea is to traverse both trees and compare value at their root node. If the value matches, we recursively check if left subtree of first tree …

Read More Check if two binary trees are identical or not | Iterative & Recursive