## 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: https://leetcode.com/problems/maximum-depth-of-binary-tree/ 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 […]

## DFS (Depth-First Search) in Binary Trees and Graphs

DFS (Depth-First Search) is an algorithm for traversing a graph. DFS starts from source vertex (graph) or root (in trees) and then visits an unvisited adjacent node v. After that it checks if node v has any adjacent node which is unvisited. If it reaches a leaf node or any node having […]

## BFS (Breadth-First Search) in Binary Trees and Graphs

BFS (Breadth-First Search) is one of the simplest algorithms for traversing a graph. Prim’s min-spanning-tree and Djikstra’s algorithm use similar ideas as BFS. BFS is also known as Level-order traversal because the algorithm discover all nodes at distance k from root before discovering any nodes at distance k+1. Pseudocode for BFS […]