Binary search tree

A binary search tree is a binary tree, with the content of all elements in the left subtree of a node are lesser than the content in the parent. Whereas all nodes content in its right subtree are greater than its parent. Let’s see below two example binary search trees. As observed in the aboveRead More »

Find root to leaf path whose sum is maximum in a binary tree

Given a binary tree with all positive integer nodes, we need to find the path from the root to a leaf whose sum is maximum in all root to leaf paths of the binary tree. The above diagram shows a simple binary tree with the maximum sum path [24, 16, 12]. This algorithm needs anRead More »

Print binary tree nodes in vertical order (vertical order traversal)

A vertical order traversal is one of the binary tree traversals, where all elements are vertically at the same level, displays as an array of elements. If someone looks at a 3D model of the binary tree from bottom to top way, all the rear nodes which are hidden by front nodes, come under theRead More »

Find verticals sum of binary tree

Given a binary tree with integer nodes in it, the aim is to find verticals sum. Let’s discuss this with the below example. As shown in the image above, each vertical level has an index, like {-2, -1, 0, 1, 2} in the above picture. The result should be the sum of all the nodesRead More »

Convert binary tree to its sum tree in-place

A binary tree with integer nodes is given, we aim to convert this binary tree to its sum tree in-place. A Sum Tree is a binary tree in which the value of the current node is a sum of data of all its descendent nodes data. Let’s see below the binary tree and its sumRead More »

Level order tree traversal

Other than usual pre, post, and in-order binary tree traversals. There are other ways to traverse a binary tree. We will see level order traversals in this article. In the level order traversal, tree nodes process level by level. There are two ways to solve this level order traversal problem. Recursive Approach In this approach,Read More »

Find height of given binary tree

The height of a binary tree is the longest distance between its root and one of its children. Let’s take the below example tree and calculate the height of it. A binary tree which is as shown in the picture has the longest distance between the root and its far most child 18 is 4.Read More »

Find given two binary trees are identical

Given two binary trees, check if all the nodes in those binary trees are the same. Recursively if two binary trees (L1, P1, R1) and (L2, P2, R2) are identical only when P1 = P2 and both (L1, L2) and (R1, R2) subtrees are also identical. Below are the pictorial representation of two identical trees.Read More »

Binary Tree data structure

A binary tree is a tree data structure with a maximum of two children in each node. Similarly, the recursive meaning of a binary tree is a tree (L, P, R) where L and R are binary trees with P as root. A binary tree node has pointed to at most another two nodes. ARead More »