Tree Question List: 1. Binary Tree traversal, Non-Recursive Pre-Order/In-Order/PostOrder (DFS) Given a binary tree, traverse the tree nodes in pre-order/in-order/post-order, iteratively. 2. Binary Tree Traversal (Threading), layer by layer (BFS) Given a binary tree, traverse the tree nodes layer by layer in order of, left-to-right, right-to-left, and ZigZag. It can be done either through one queue+ one stack or stack+stack. Problem 3.10 of “Programming Beauty” or http://tech-queries.blogspot.com/2011/11/printbinary-tree-in-zig-zag-level.html We usually use a queue for level order traversal of a queue. If we want to use the same here, how will we change direction of traversal on every alternate level? If we think logically then we will see that we need stack in place of queue. Now again a single stack won’t serve our purpose here, we need 2 stacks: stack1 for right to left and stack2 for left to right. For identifying, whether we need to put data in stack1 or stack2, we need a flag (left2right) to indicate direction (after a level finishes). Initially, left2right is true and we push root node in stack1 and that’s our beginning point. Now since after root node, first level fished, we toggle left2right. Until stack1 is empty, we pop values from stack1 and push values based on left2right flag. Once stack1 finishes, we toggle left2right again and switch stack’s role. For clarification, when left2right is true, push left node first in stack and then right node. Similarly left2right is false, push right node first in stack and then left node. 3. Lowest Ancestor of Binary Tree Given a binary tree, find the lowest common ancestor of two given nodes in the tree. http://www.leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html 4. Construct Binary Tree from Pre-order/In-order/Post-order Given pre-order/in-order/post-order traversal of binary tree nodes, construct the binary tree. A different version of the problem would be to serialize a binary tree to transmit over the Internet. http://www.leetcode.com/2011/04/construct-binary-tree-from-inorder-and-preorderpostorder-traversal.html 5. Reconstruct tree from pre-order traversal http://tech-queries.blogspot.com/2011/06/reconstruct-tree-from-pre-order.html A tree has a special property where leaves are represented with ‘L’ and non-leaf with ‘N’. Each node has either 0 or 2 children. If given preorder traversal of this tree, construct the tree. Example: Given Pre Order string => NLNLL 6. Largest Binary Search Tree in a Binary Tree Given a binary tree, find the largest Binary Search Tree (BST), where largest means BST with largest number of nodes in it. The largest BST may or may not include all of its descendants. http://www.leetcode.com/2010/11/largest-binary-search-tree-bst-in_22.html 7. Longest Distance of two nodes of a Binary tree Define “Distance” of two nodes as the number of edges between these two nodes. Find the largest distance between the nodes in the tree. Problem 3.8 of “Programming Beauty” 8. DFS (Pre-Order/In-Order/Post-Order) threading a given binary tree 9. Mirror/Duplicate/Demolition a given (binary) tree 10. Comparison between two given (binary) trees 11. Flatten a binary tree 12. Reverse a tree Reverse a binary tree. If the node is left child for parent, then convert its right child to its parent, and left child to NULL. If the node is right child for parent, convert its left child to its parent and right child to NULL. After revert, the tree becomes a graph. 13. Serialize a binary tree so it can be read/write from the file Serialize a binary tree and save it into a file. Then restore it from the file. The tree is not binary search tree. You can assume the treenode->Value is integer. http://www.leetcode.com/2010/09/serializationdeserialization-of-binary.html 14. Convert a sorted array to balanced BST (Balancing BST) http://www.leetcode.com/2010/11/convert-sorted-array-into-balanced.html 15. Convert BST to sorted doubly linked-list or sorted array Take a binary tree and rearrange left and right pointers to make a circular doubly linked list Or Convert a BST to a sorted circular doubly-linked list in-place. http://www.leetcode.com/2010/11/convert-binary-search-tree-bst-to.html Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. 16. Print Edge Nodes (Boundary) of a Binary Tree Print all edge nodes of a complete binary tree anti-clockwise. That is all the left most nodes starting at root, then the leaves left to right and finally all the rightmost nodes. In other words, print the boundary of the tree. Variant: Print the same for a tree that is not complete. http://www.leetcode.com/2010/10/print-edge-nodes-boundary-of-binary.html 17. A binary tree problem – Populating next right pointers in each node Given a binary tree struct Node { Node* leftChild; Node* rightChild; Node* nextRight; } Populate the nextRight pointers in each node. You may assume that it is a full binary tree (ie, each node other than the leaves has two children.) http://www.leetcode.com/2010/03/first-on-site-technical-interview.html 18. Print BST traversal path (GOOG) Given a BST and two nodes' value on the tree, print the "UP", "LEFT" or "RIGHT" from S to T: 19. If it goes up, print "UP" If it goes down to left child, print "LEFT" If it goes down to right child, print "RIGHT" Deep Copy Binary Tree with Random pointer Given a tree node structure as follows. Each Random points to any validate node in the tree. struct Node { Node* leftChild; Node* rightChild; Node* Random; } Deep copy the binary tree and make the Random pointer point to the new tree node in the same position as the random pointer in old tree. 20. Find internal loop in a binary tree Given a binary tree, there is a loop inside the tree, i.e., one of the nodes is pointing to its ancestor. Design an algorithm to find out this loop. (From mitbbs, amazon phone interview question) (if to mark the filed explicitly or use the hash table, there is no trick here. Do I miss something here?) 21. Check if identical BSTs from given number streams You are given 2 number streams. You need to find whether they will create the same BST or not. http://tech-queries.blogspot.com/2011/06/check-if-identical-bsts-from-given.html Example: Array1:10 5 20 15 30 Array2:10 20 15 30 5 Result: True Array1:10 5 20 15 30 Array2:10 15 30 20 5 Result: False (see corresponding trees below)