Top view of a binary tree
In this chapter we shall learn how to print top view of a binary tree. Problem Statement: You are given a root node of the tree print the top view of the tree. For example: Consider the below tree: So the top view will be d b a c g Let’s see how do we […]
Boundary Traversal of Binary Tree
We shall solve boundary traversal of binary tree in this tutorial. Problem Statement: Given a binary tree, list all the nodes that come in the boundary. Consider the tree as below: So the boundary traversal will be as below: a b d h c g l i j f k So how did we arrive […]
Bottom view of binary Tree
In this tutorial, we shall solve how to print bottom view of a binary tree. Problem statement: You are given a binary tree; you need to print the bottom view of a binary tree. Consider the tree: So the solution would be h d b f i g j This problem can be solved in […]
Bitwise and of number range
You are given a number range, you need to find the bitwise and of all the numbers in the range. Example: [5, 7] The bitwise and of 5, 6, 7 will be 4. The solution is very simple, once you understand the concept below: Let’s take small example and try to understand. We shall find […]
Hamming Distance
You are given 2 integers, and you need to return the count at which the bits are different in their binary form. Consider 8 and 1 As highlighted in the image, there are 2 positions where the bits are different. Hence output should be 2. We shall solve this by bit manipulation. Before solving the […]
Counting set bits in a number
You are given a number; you need to create an array to fill the number of 1’s present in their binary format. Example: Input n = 3 Output = [0, 1, 1, 2] Because for 0, the number of set bits is 0. For 1, it’s binary format is 001, set bits is 1. For […]
Missing Number
Problem statement: You are given an array of numbers from 1 to n, in any order, one number will be missing. You need to find a missing number. Example: Input: [0, 1, 3] Output: 2 We can solve this problem by 2 methods: 1. Finding the sum of all the numbers 2. Using xor Method […]
Power of 2
Given a number, you need to check if the number is a power of 2, return true or false accordingly. Example: Input: 1 Output: True Because 2^0 is 1 Input: 15 Output: False Because 15 is not a power of 2. Input: 8 Output: True Because 2^3 is 8 We can solve this by 2 […]
Count the number of set bits
Problem statement: You are given a number, return the number of set bits if that number, when represented that number in binary format. Example: If the input number is 10, it’s binary representation is 1010. The number of set bits are 2. This problem can be solved in 2 ways. 1. By using in build […]
Reverse Bits
Problem Statement: You are given a number as input. You need to reverse the bits in it’s binary form and print the output. The maximum length is a 32-bit unsigned integer. Example: Let’s see how to solve this problem Let us take a simple problem and solve; Input: 1010001 Output: 1000101 To arrive at the […]