Root to leaf path – LeetCode Solution [Easy]

Root to leaf path problem statement is:Given the root of a binary tree, return all root-to-leaf paths in any order. A leaf is a node with no children. Example 1: Input: root = [3,9,20,null,null,15,7] Output: [“3->9″,”3->20->15″,”3->20->7”] Root to leaf path – Leetcode Solution Approach The idea is to use the DFS Traversal of the binary tree to […]

Lowest Common Ancestor in a Binary Tree in C++

Problem Statement Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Definition of LCA (according to Wikipedia) The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants. For the given Binary tree, if info[key1] = 15 and […]

Convert Sorted Array to Binary Search Tree in C++ – LeetCode Solution [Easy]

Problem Statement Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. Example 1: Input: nums = [-10,-3,0,5,9] Output: [0,-3,9,-10,null,5] Approach This problem […]

Print Binary tree in vertical order

Problem Statement Given a Binary tree, print it in vertical order from left to right. Vertical order for the above Binary tree is: Vertical order(root): 2 3 1 5 4 7 8 We could solve this problem by performing a breadth-first or level order traversal on the given tree and using […]

Binary Search Tree in C/C++[2]

In the last post we learnt about Binary Search Trees and basic operations like insert and search on them. In this post we’ll learn about some complex operations on them. Implementing Binary Search Tree operations in C/C++ Finding Minimum Element Tree-Minimum (root)1. while (left[root] != NULL)2. root = left[root]3. return root […]