Binary Search Tree in C/C++

Binary search tree (BST) is a kind of Binary tree that satisfies the following property for every node x: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then info[y] <= info[x]. If y is a node in the […]

How to find if a Tree is a subtree of the given tree in C++

Given the roots of two binary trees root and rootSub,  check if there is a subtree of root with the same structure and node values as rootSub A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. Pseudocode 1. Traverse the given binary […]

How to print ancestors of a node in Binary tree using C++

Problem statement is simple, you’re given a root of a binary tree and a key k. You need to print the ancestors of k in the given binary tree For the given Binary tree, if k = 31, it should print: 10 21 Pseudocode 1. Traverse the given binary tree in […]

How to find the maximum height of a Binary tree – LeetCode Solution [Easy]

Given the root of a binary tree, return its maximum depth. A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Problem Link: Height of an empty Binary tree is -1. And as you can see, height of above tree is […]

How to find if two Binary trees are mirror images?

Mirror images mean right and left child of the two trees are interchanged. Thus, left child of first tree is right child of second tree. Consider the following image: As you could see in the above image: Roots of Tree 1 and Tree 2 are same Left child of Tree 1 […]