Close

Creating a balanced binary search tree from sorted array

Given a sorted array, how do we create binary search which height balanced. For example the array [1,2,3,4,5] must be transformed to the following binary tree                            3                /                 2    4              /                     1        5 We can use the divide and conquer approach to solve this problem. To create a…

Checking if a binary tree is balanced

Given a binary tree, how do we check if it is balanced or not? We say that a binary tree is balanced if height difference of left  and the right sub-trees is at most 1 at  any node. Consider the following examples   1          1               1             1  /                       /              …

Longest sub array with zero sum

Given an array of integers (positive and negative), how do we find a longest sub-array with zero sum? For example, consider the example [5, 0, -1, 3, -2, 4], the longest sub array with zero sum contains 4 elements.  The straight forward approach is to check the sum of all possible arrays and find out…

Type casting in C++ – Part 1

Type casting frequently arises in real life programming. This post concentrates on type conversion of fundamental data types. Type casting is the process of converting from one data type to another. In C++, type casting is automatic in some situations. These are called implicit conversions. For example char c = ‘A’; int ch_code = c;…