Browsing the topic Trees
There are two conventions to define height of Binary Tree 1) Number of nodes on longest path from root to the deepest node. 2) Number of edges on longest path from root to the deepest node.
Read More »Consider a coding system for alphabets to integers where ‘a’ is represented as 1, ‘b’ as 2, .. ‘z’ as 26. Given an array of digits (1 to 9) as input, write a function that prints all valid interpretations of input array.
Read More »Write a function to detect if two trees are isomorphic. Two trees are called isomorphic if one of them can be obtained from other by a series of flips, i.e. by swapping left and right children of a number of nodes.
Read More »Given a dictionary of words and an input string, find the longest prefix of the string which is also a word in dictionary.
Read More »In the previous post, we introduced B-Tree. We also discussed search() and traverse() functions.
Read More »Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL.
Read More »Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST. For example, consider the following BST and range [-10, 13].
Read More »Given Linked List Representation of Complete Binary Tree, construct the Binary tree. A complete binary tree can be represented in an array in the following approach.
Read More »We have discussed level order traversal of a post in previous post. The idea is to print last level first, then second last level, and so on. Like Level order traversal, every level is printed from left to right.
Read More »Given a Balanced Binary Search Tree and a target sum, write a function that returns true if there is a pair with sum equals to target sum, otherwise return false.
Read More »Given a Balanced Binary Search Tree (BST), write a function isTripletPresent() that returns true if there is a triplet in given BST with sum equals to 0, otherwise returns false.
Read More »We have discussed a simple iterative postorder traversal using two stacks in the previous post. In this post, an approach with only one stack is discussed.
Read More »We have discussed iterative inorder and iterative preorder traversals. In this post, iterative postorder traversal is discussed which is more complex than the other two traversals (due to its nature of non-tail recursion, there is an extra statement after the final recursive call to itself).
Read More »Given a Binary Tree, find size of the Largest Independent Set(LIS) in it. A subset of all tree nodes is an independent set if there is no edge between any two nodes of the subset. For example, consider the following binary tree. The largest independent set(LIS) is {10, 40, 60, 70, 80} and size of [...]
Read More »We have introduced segment tree with a simple example in the previous post. In this post, Range Minimum Query problem is discussed as another example where Segment Tree can be used.
Read More »Let us consider the following problem to understand Segment Trees. We have an array arr[0 . . . n-1]. We should be able to 1 Find the sum of elements from index l to r where 0 < = l
Read More »A ternary search tree is a special trie data structure where the child nodes of a standard trie are ordered as a binary search tree.
Read More »A complete binary tree is a binary tree where each level ‘l’ except the last has 2^l nodes and the nodes at the last level are all left aligned. Complete binary trees are mainly used in heap based data structures.
Read More »Using Morris Traversal, we can traverse the tree without using stack and recursion. The algorithm for Preorder is almost similar to Morris traversal for Inorder.
Read More »Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
10 Comments | Filed under TreesGiven a Binary Search Tree (BST), convert it to a Binary Tree such that every key of the original BST is changed to key plus sum of all greater keys in BST
Read More »Given a Binary Tree, write an iterative function to print Preorder traversal of the given binary tree.
Read More »There are numerous applications we need to find floor (ceil) value of a key in a binary search tree or sorted array.
Read More »Given preorder traversal of a binary search tree, construct the BST. For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be root of following tree.
Read More »Given preorder traversal of a binary search tree, construct the BST. For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be root of following tree.
Read More »Construct Full Binary Tree from given preorder and postorder traversals
20 Comments | Filed under TreesGiven two arrays that represent preorder and postorder traversals of a full binary tree, construct the binary tree.
Read More »