Pages

Monday 3 December 2012

A comprehensive list of all problems on Binary Search Trees

  1. Tree Traversals InOrder - non decreasing order, PreOrder - used to create a copy, PostOrder - delete the tree
  2. Iterative Pre-Order Traversal : This is a nice question. When you cannot use recursion for such a problem, you need a stack to simulate the recursion.
  3. Level Order Traversal : Before this you should be prepared with the stacks and queue api's first. Besides, study all CTCI questions on Binary Search Trees.
  4. http://www.cs.duke.edu/courses/spring00/cps100/assign/trees/
  5. Find the most weighted node in a BST.
  6. Diameter of a BST
  7. Connect nodes at same level
  8. Children Sum Property
  9. Convert a Binary tree so that it follows the children sum property : look at the soln in the copy, it is more elegant as it doesn't use the increment function. It takes care of both the cases while returning. Nice thinking.
  10. Construct Binary tree from the inorder traversal and following a special property : First, I couldn't understand why this problem is special. Thinking about binary trees after a long time. Is an inorder traversal enough to get a binary tree back ? No right. There might be many combinations that are possible from the inorder traversal. Your work is to print the binary tree which follows the special property that every node has a value which is greater than both the nodes in the left and the right children. One thing, that I get right away is the max element in the inorder traversal is the root. Recursively thinking, the array to the left of it is the left subtree and the array to the right of it is the right subtree. Done.
  11. http://discuss-prog.blogspot.com/2012/08/interesting-problems-on-trees-3.html
  12. http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/
  13. Program to check whether a binary tree is a BST or not ?  The first approach that I thought was that check if the left subtree is a bst, right subtree is a bst and the root is lesser than the right subtree and greater than the left subtree. But it is inefficient. The best way is to do an inorder traversal and check whether it is sorted or not.
  14. The most awesomest code for LCA of a binary tree from leetcode
  15. Do an inorder traversal of the tree and the result should be in sorted order.
  16. Convert a BST to a doubly linked list - look at the leetcode solution which converts it to a circular linked list. Then write the solution for a normal linkedlist
  17. Construct BST from pre-order traversal : here the tree can be constructed from pre-order traversal because we know that it is a BST. This extra criterion helps us.
  18. However, in these lines of questions whether a tree can be constructed from the given traversals, it can only be done if one of the traversals are inorder. It cannot be done even if preorder, postorder and levelorder traversals are given and we only know that the tree is a Binary Tree and not a BST.
  19. Given Inorder Traversal of a Special Binary Tree in which key of every node is greater than keys in left and right children, construct the Binary Tree and return root. In this question, only one traversal is given and the tree is a binary tree, hence, this question will have to follow a property that the each node is greater than both its children.
  20. http://www.geeksforgeeks.org/construct-a-special-tree-from-given-preorder-traversal/
  21. http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
  22. http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
  23. http://www.geeksforgeeks.org/construct-a-special-tree-from-given-preorder-traversal/ - use static int if you don't feel like pointer to an integer in a function
  24. http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-traversals/ - it is not possible to construct a binary tree from preorder and postorder traversal
  25. http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-traversals/ - I have not seen this code yet and will be good practice in fresh mind to solve this problem. Even if you are woken up in the middle of the night, you should be able to solve this problem. Here also we can see, that post order and pre-order traversals are given. So this BT will have ambiguity. However, we have the extra condition that it is a full Binary Tree. A full binary tree has the property that it has 2 elements at all the levels. So we know that in preorder the first element is the root and the second element is the left subtree. So we can use this information to find the left subtree and the right subtree from post order traversals.
  26. http://www.geeksforgeeks.org/print-nodes-at-k-distance-from-root/
  27. http://www.geeksforgeeks.org/write-a-c-program-to-get-count-of-leaf-nodes-in-a-binary-tree/
  28. http://www.geeksforgeeks.org/root-to-leaf-path-sum-equal-to-a-given-number/ - also print the path. Since, root to the leaf node has to be seen, this is going to be preorder traversal. If you have to store the path, then you have to use a stack and keep inputting the roots into the stack. Every time you come out of a left subtree and from a right subtree, you need to pop out the stack except the root, because that is what is required for the next path on may be the right subtree from a parent of the root.
  29. http://codercareer.blogspot.com/2011/09/no-06-post-order-traversal-sequences-of.html - Here also the tree can be constructed from the post order traversal because we know that it is a BST. If it were a binary tree then we would need the inorder traversal to construct the tree.
  30. http://coding-interviewq.blogspot.com/2013/02/trim-given-bst-based-on-min-and-max.html - its all about formulating it recursively
  31. http://coding-interviewq.blogspot.com/2013/02/how-to-get-prev-element-in-inorder.html - Key concept - its all about traversal
  32. Its all about search - http://coding-interviewq.blogspot.com/2013/02/bst-problems-where-underlying-thing.html
  33. http://www.geeksforgeeks.org/iterative-preorder-traversal/ - Iterative traversal can be done using a stack or a queue. A recursion is a function call and architecturally a function call is implemented using a stack. So, put one element in the stack, pop it and check for its children. In the next iteration in the loop pop one of the children and repeat the process.
  34. http://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/
  35. df
WIP. Please add your  questions on the comments and I will update the list.

No comments:

Post a Comment