binary-tree

Blog Cover Image
AVL Tree (Self-Balancing Binary Search Tree)

AVL Trees named after its inventors Adelson-Velsky and Landis, is a unique variation of the Binary Search Tree, which is a self-balancing BST. The property of AVL Trees is that they automatically attain the tree's minimum possible height after executing any operation.

Blog Cover Image
Introduction to Binary Tree: Properties, Types, Representation and Application

Trees can have any number of children, but the simplest and most common type of tree is a binary tree, where each node has at most two child nodes (called left child and right child). In other words, a node in a binary tree can have 0 or 1 or 2 child nodes.

Blog Cover Image
Validate Binary Search Tree: Check if a Binary Tree is BST or not

Given the root of a binary tree, write a program to check whether it is a valid binary search tree (BST) or not. A BST is valid if all nodes in the left subtree have values less than the node’s value, all nodes in the right subtree have values greater than the node’s value, and both left and right subtrees are also binary search trees.

Blog Cover Image
Segment Trees Part 2: Point Update, Range Update and Lazy Propagation

In this blog, we will learn how to use segment trees for efficient point and range updates. For the point update query, update(idx, val), we need to increment the element at the index idx from the original array by val, i.e. arr[idx] = arr[idx] + val. For the range update query, update(l, r, val), we must increment all the elements from index l to r from the original array by val.

Blog Cover Image
Introduction to Heap in Data Structures and Algorithms

A heap is a complete binary tree structure where each element satisfies a heap property. We learn two types of the heap in programming: 1) max-heap, which satisfies the max heap property, and 2) min-heap, which satisfies the min-heap property. We use the heap to implement the priority queue efficiently.

Blog Cover Image
Segment Tree Implementation Part 1: Build and Range Query Operations

A Segment Tree is a data structure used to answer multiple range queries on an array efficiently. Also, it allows us to modify the array by replacing an element or an entire range of elements in logarithmic time. This blog will focus on the build and query operations on segment trees.

Blog Cover Image
Recursive Tree Traversals of a Binary Tree: Preorder, Inorder and Postorder Traversal

To process data stored in a binary tree, we need to traverse each tree node, and the process to visit all nodes is called binary tree traversal. In this blog, we will be discussing three popular recursive tree traversals: preorder, inorder and postorder traversals. These traversals are also called depth-first search traversal or dfs traversal in data structures.

Blog Cover Image
Left View of a Binary Tree

Write a program to find the left view of the given binary tree. The left view of a binary tree is a group of nodes visible when viewed from the left side. In other words, the nodes present in the left view will be the first node of each level. This is an excellent problem to learn problem-solving using DFS and BFS traversal.

Blog Cover Image
Fenwick Tree (Binary Indexed Tree)

A Fenwick tree is a data structure that efficiently updates elements and calculates prefix sums in an array. It takes O(logn) time for both updates and range sum queries. It is also called Binary Indexed Tree (BIT), which was first described in the paper titled “A new data structure for cumulative frequency tables” (Peter M. Fenwick, 1994).

Blog Cover Image
Kth Largest Element in a BST

Given the root of a BST and an integer k, write a program to find the kth largest value among all the nodes' values in the tree. This is an excellent problem to learn problem-solving using inorder traversal and data structure augmentation (storing extra information inside BST nodes for solving a problem).

Blog Cover Image
Minimum Absolute Difference in a BST

Given the root of a Binary Search Tree (BST), write a program to find the absolute minimum difference between the values of any two nodes in the tree. Here node values in the tree can be positive, negative, or zero. This is an excellent problem to learn problem-solving using in-order traversal in a BST.

Blog Cover Image
Maximum depth or height of a binary tree

Given a binary tree, write a program to find its height. The height or depth of a binary tree is equal to the count of nodes on the longest path from the root to the leaf, i.e., the max number of nodes from the root to the most distant leaf. This is an excellent problem to learn problem-solving using DFS and BFS traversal.

Blog Cover Image
Merge Two Binary Trees

Given two binary trees, merge them into a new binary tree such that if two nodes overlap then add them and put them into the new tree at the same location else put the non-NULL node in the new tree.

Blog Cover Image
Minimum Depth of a Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. The path has to end on a leaf node.

Blog Cover Image
Iterative Binary Tree Traversal using Stack: Preorder, Inorder and Postorder

In recursive DFS traversals of a binary tree, we have three basic elements to traverse— root, left subtree, and right subtree. The traversal order depends on the order in which we process the root node. Here recursive code is simple and easy to visualize — only one function parameter and 3–4 lines of code. So critical question would be — How can we convert it into iterative code using stack? To simulate the recursive traversal into an iterative traversal, we need to understand the flow of recursive calls.

Blog Cover Image
Level Order Traversal (BFS Traversal) of a Binary Tree 

Level order traversal accesses nodes in level by level order. This is also called breadth-first search traversal or BFS traversal. Here we start from the root node and process it, then process all the nodes at the first level, then process all the nodes at the second level, and so on. In other words, we explore all nodes at the current level before moving on to the nodes at the next level.

Our weekly newsletter

Subscribe to get free weekly content on data structure and algorithms, machine learning, system design, oops design and mathematics.