The Wayback Machine - https://web.archive.org/web/20240926150244/https://www.geeksforgeeks.org/find-the-minimum-element-in-a-binary-search-tree/
Open In App

Find the node with minimum value in a Binary Search Tree

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
70.95%
1.9L

Given the root of a Binary Search Tree. The task is to find the minimum valued element in this given BST.

Example: 

Input:

ex-1

Output: 1
Explanation: The minimum element in the given BST is 1.

Input:

ex-2

Output: 2
Explanation: The minimum element in the given BST is 2

[Naive Approach] Using Inorder Traversal – O(n) Time and O(n) Space

The idea is to use the property of BST which says inorder traversal of a binary search tree always returns the value of nodes in sorted order. So the 1st value in the sorted vector will be the minimum value which is the answer.

Below is the implementation of the above approach:

C++
// C++ code to find minimum value in BST
// using inorder traversal
#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node *left, *right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

// Recursive function to solve and store elements 
// in a vector
void inorder(Node* root, vector<int>& sortedInorder) {
  
    // Base Case
    if (root == nullptr) return;

    // Traverse left subtree
    inorder(root->left, sortedInorder);

    // Store the current node's data
    sortedInorder.push_back(root->data);

    // Traverse right subtree
    inorder(root->right, sortedInorder);
}

// Function to find the minimum value in BST
int minValue(Node* root) {
    if (root == nullptr) {
        return -1;
    }
    
    vector<int> sortedInorder;
    
    // Call the recursive inorder function
    inorder(root, sortedInorder);
    
    // Return the first element, which is the minimum
    return sortedInorder[0];
}

int main() {

    // Representation of input binary search tree
    //        5
    //       / \
    //      4   6
    //     /     \
    //    3       7
    //   / 
    //  1
    Node* root = new Node(5);
    root->left = new Node(4);
    root->right = new Node(6);
    root->left->left = new Node(3);
    root->right->right = new Node(7);
    root->left->left->left = new Node(1);

    cout << minValue(root) << "\n";

    return 0;
}
C
// C code to find minimum value in BST
// using inorder traversal
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

// Recursive function to traverse the tree 
// and store elements in a vector
void inorder(struct Node* root, int *sortedInorder, 
                                          int *index) {
  
    // Base Case
    if (root == NULL) return;

    // Traverse left subtree
    inorder(root->left, sortedInorder, index);

    // Store the current node's data
    sortedInorder[(*index)++] = root->data;

    // Traverse right subtree
    inorder(root->right, sortedInorder, index);
}

// Function to find the minimum value in a BST
int minValue(struct Node* root) {
    if (root == NULL) {
        return -1;
    }
    
    // Create an array to hold inorder elements
    int sortedInorder[20000];  
    int index = 0;
    
    // Call the recursive inorder function
    inorder(root, sortedInorder, &index);
    
    // Return the first element, which is the minimum
    return sortedInorder[0];
}

struct Node* createNode(int val) {
    struct Node* node
      = (struct Node*)malloc(sizeof(struct Node));
    node->data = val;
    node->left = node->right = NULL;
    return node;
}

int main() {

    // Representation of input binary search tree
    //        5
    //       / \
    //      4   6
    //     /     \
    //    3       7
    //   /
    //  1
    struct Node* root = createNode(5);
    root->left = createNode(4);
    root->right = createNode(6);
    root->left->left = createNode(3);
    root->right->right = createNode(7);
    root->left->left->left = createNode(1);

    printf("%d\n", minValue(root));

    return 0;
}
Java
// Java code to find minimum value in BST
// using inorder traversal
import java.util.ArrayList;

class Node {
    int data;
    Node left, right;

    Node(int val) {
        data = val;
        left = right = null;
    }
}

class GfG {

    static void inorder(Node root, 
                     ArrayList<Integer> sortedInorder) {
      
        // Base Case
        if (root == null) return;

        // Traverse left subtree
        inorder(root.left, sortedInorder);

        // Store the current node's data
        sortedInorder.add(root.data);

        // Traverse right subtree
        inorder(root.right, sortedInorder);
    }

    static int minValue(Node root) {
      
        if (root == null) {
            return -1;
        }

        // Create an ArrayList to hold inorder elements
        ArrayList<Integer> sortedInorder 
                                 = new ArrayList<>();

        // Call the recursive inorder function
        inorder(root, sortedInorder);

        // Return the first element, which is the minimum
        return sortedInorder.get(0);
    }

    public static void main(String[] args) {

        // Representation of input binary search tree
        //        5
        //       / \
        //      4   6
        //     /     \
        //    3       7
        //   /
        //  1
        Node root = new Node(5);
        root.left = new Node(4);
        root.right = new Node(6);
        root.left.left = new Node(3);
        root.right.right = new Node(7);
        root.left.left.left = new Node(1);

        System.out.println(minValue(root));
    }
}
Python
# Python code to find minimum value in BST
# using inorder traversal

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

def inorder(root, sorted_inorder):
  
    # Base Case
    if root is None:
        return

    # Traverse left subtree
    inorder(root.left, sorted_inorder)

    # Store the current node's data
    sorted_inorder.append(root.data)

    # Traverse right subtree
    inorder(root.right, sorted_inorder)

def minValue(root):
    if root is None:
        return -1

    # Using a list to hold inorder elements
    sorted_inorder = []  

    # Call the recursive inorder function
    inorder(root, sorted_inorder)

    # Return the first element, which is the minimum
    return sorted_inorder[0]

if __name__ == "__main__":
  
    # Representation of input binary search tree
    #        5
    #       / \
    #      4   6
    #     /     \
    #    3       7
    #   /
    #  1
    root = Node(5)
    root.left = Node(4)
    root.right = Node(6)
    root.left.left = Node(3)
    root.right.right = Node(7)
    root.left.left.left = Node(1)

    print(minValue(root))
C#
// C# code to find minimum value in BST
// using inorder traversal
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GfG {

    static void Inorder(Node root, List<int> sortedInorder) {
      
        // Base Case
        if (root == null) return;

        // Traverse left subtree
        Inorder(root.left, sortedInorder);

        // Store the current node's data
        sortedInorder.Add(root.data);

        // Traverse right subtree
        Inorder(root.right, sortedInorder);
    }

    static int MinValue(Node root) {
        if (root == null) {
            return -1;
        }

        // Create a list to hold inorder elements
        List<int> sortedInorder = new List<int>();

        // Call the recursive inorder function
        Inorder(root, sortedInorder);

        // Return the first element, which is the minimum
        return sortedInorder[0];
    }

    static void Main(string[] args) {

        // Representation of input binary search tree
        //        5
        //       / \
        //      4   6
        //     /     \
        //    3       7
        //   /
        //  1
        Node root = new Node(5);
        root.left = new Node(4);
        root.right = new Node(6);
        root.left.left = new Node(3);
        root.right.right = new Node(7);
        root.left.left.left = new Node(1);

        Console.WriteLine(MinValue(root));
    }
}
JavaScript
// JavaScript code to find minimum value in BST
// using inorder traversal

class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

function inorder(root, sortedInorder) {
  
    // Base case
    if (root === null) return;

    // Traverse left subtree
    inorder(root.left, sortedInorder);

    // Store the current node's data
    sortedInorder.push(root.data);

    // Traverse right subtree
    inorder(root.right, sortedInorder);
}

function minValue(root) {
    if (root === null) {
        return -1;
    }

    // Create an array to hold inorder elements
    let sortedInorder = []; 

    // Call the recursive inorder function
    inorder(root, sortedInorder);

    // Return the first element, which is the minimum
    return sortedInorder[0];
}

// Representation of input binary search tree
//        5
//       / \
//      4   6
//     /     \
//    3       7
//   /
//  1
let root = new Node(5);
root.left = new Node(4);
root.right = new Node(6);
root.left.left = new Node(3);
root.right.right = new Node(7);
root.left.left.left = new Node(1);

console.log(minValue(root));

Output
1

Time Complexity: O(n), since we traversed through all the elements in a BST.
Auxiliary Space: O(n), we are storing all the n nodes in an array.

[Expected Approach] Iterative Approach – O(n) Time and O(1) Space

The idea is that in a Binary Search Tree (BST), the left child of a node is always smaller than the root. This ensures that the node whose left pointer is NULL must hold the minimum value in the tree. The leftmost node will always contain the smallest element.

Below is the implementation of the above approach:

C++
// C++ code to find minimum value in BST
// using iteration
#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node *left, *right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

int minValue(Node* root) {
  
    // base case
    if (root == nullptr) {
        return -1;
    }

    Node* curr = root;

    // leftmost node is minimum so we move in BST till
    // left node is not nullptr
    while (curr->left != nullptr) {
        curr = curr->left;
    }

    // returning the data at the leftmost node
    return curr->data;
}

int main() {

    // Representation of input binary search tree
    //        5
    //       / \
    //      4   6
    //     /     \
    //    3       7
    //   / 
    //  1
    Node* root = new Node(5);
    root->left = new Node(4);
    root->right = new Node(6);
    root->left->left = new Node(3);
    root->right->right = new Node(7);
    root->left->left->left = new Node(1);

    cout << minValue(root) << "\n";

    return 0;
}
C
// C code to find minimum value in BST
// using iteration
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

// Function to find minimum value in BST
int minValue(struct Node* root) {
  
    // base case
    if (root == NULL) {
        return -1;
    }

    struct Node* curr = root;

    // leftmost node is minimum, so move 
    // till left is not NULL
    while (curr->left != NULL) {
        curr = curr->left;
    }

    // returning the data at the leftmost node
    return curr->data;
}

struct Node* createNode(int val) {
    struct Node* node 
            = (struct Node*)malloc(sizeof(struct Node));
    node->data = val;
    node->left = node->right = NULL;
    return node;
}

int main() {

    // Representation of input binary search tree
    //        5
    //       / \
    //      4   6
    //     /     \
    //    3       7
    //   /
    //  1
    struct Node* root = createNode(5);
    root->left = createNode(4);
    root->right = createNode(6);
    root->left->left = createNode(3);
    root->right->right = createNode(7);
    root->left->left->left = createNode(1);

    printf("%d\n", minValue(root));

    return 0;
}
Java
// Java code to find minimum value in BST
// using iteration
class Node {
    int data;
    Node left, right;

    Node(int val) {
        data = val;
        left = right = null;
    }
}

public class GfG {

    public static int minValue(Node root) {
      
       // base case
        if (root == null) {
            return -1;
        }

        Node curr = root;

        // leftmost node is minimum, so move till 
        // left is not null
        while (curr.left != null) {
            curr = curr.left;
        }

        // returning the data at the leftmost node
        return curr.data;
    }

    public static void main(String[] args) {

        // Representation of input binary search tree
        //        5
        //       / \
        //      4   6
        //     /     \
        //    3       7
        //   /
        //  1
        Node root = new Node(5);
        root.left = new Node(4);
        root.right = new Node(6);
        root.left.left = new Node(3);
        root.right.right = new Node(7);
        root.left.left.left = new Node(1);

        System.out.println(minValue(root));
    }
}
Python
# Python code to find minimum value in BST
# using using iterationl
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


# Function to find the minimum value in BST
def minValue(root):
    # base case
    if root is None:
        return -1

    curr = root

    # leftmost node is minimum, so move 
    # till left is not None
    while curr.left is not None:
        curr = curr.left

    # returning the data at the leftmost node
    return curr.data

if __name__ == "__main__":
  
    # Representation of input binary search tree
    #        5
    #       / \
    #      4   6
    #     /     \
    #    3       7
    #   /
    #  1
    root = Node(5)
    root.left = Node(4)
    root.right = Node(6)
    root.left.left = Node(3)
    root.right.right = Node(7)
    root.left.left.left = Node(1)

    print(minValue(root))
C#
// C# code to find minimum value in BST
// using iteration
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GfG {

    static int minValue(Node root) {
      
        // base case
        if (root == null) {
            return -1;
        }

        Node curr = root;

        // leftmost node is minimum, so move 
        // till left is not null
        while (curr.left != null) {
            curr = curr.left;
        }

        // returning the data at the leftmost node
        return curr.data;
    }

    static void Main(string[] args) {

        // Representation of input binary search tree
        //        5
        //       / \
        //      4   6
        //     /     \
        //    3       7
        //   /
        //  1
        Node root = new Node(5);
        root.left = new Node(4);
        root.right = new Node(6);
        root.left.left = new Node(3);
        root.right.right = new Node(7);
        root.left.left.left = new Node(1);

        Console.WriteLine(minValue(root));
    }
}
JavaScript
// Javascript code to find minimum value in BST
// using iteration
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to find the minimum value in BST
function minValue(root) {

    // base case
    if (root === null) {
        return -1;
    }

    let curr = root;

    // leftmost node is minimum, so move till 
    // left is not null
    while (curr.left !== null) {
        curr = curr.left;
    }

    // returning the data at the leftmost node
    return curr.data;
}

// Representation of input binary search tree
//        5
//       / \
//      4   6
//     /     \
//    3       7
//   /
//  1
let root = new Node(5);
root.left = new Node(4);
root.right = new Node(6);
root.left.left = new Node(3);
root.right.right = new Node(7);
root.left.left.left = new Node(1);

// Output the minimum value in the BST
console.log(minValue(root));

Output
1

Time Complexity: O(h), where h is the height of the BST. Worst case happens for left skewed trees, in that case complexity becomes O(n).
Auxiliary Space: O(1), we are not using any extra memory.

[Alternate Approach] Using Recursion- O(n) Time and O(n) Space

The idea is to just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with the minimum value. Please refer to Find the node with minimum value in a Binary Search Tree using recursion for implementation.



Previous Article
Next Article

Similar Reads

Find the node with minimum value in a Binary Search Tree using recursion
Given the root of a Binary Search Tree, the task is to find the minimum valued element in this given BST. Examples: Input: Output: 1Explanation: The minimum element in the given BST is 1. Input: Output: 2Explanation: The minimum element in the given BST is 2. Approach: The idea is to recursively traverse the Binary Search Tree (BST) starting from t
6 min read
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Minimum swap required to convert binary tree to binary search tree
Given the array representation of Complete Binary Tree i.e, if index i is the parent, index 2*i + 1 is the left child and index 2*i + 2 is the right child. The task is to find the minimum number of swap required to convert it into Binary Search Tree. Examples: Input : arr[] = { 5, 6, 7, 8, 9, 10, 11 } Output : 3 Binary tree of the given array: Swap
8 min read
Find the node with maximum value in a Binary Search Tree using recursion
Given a Binary Search Tree, the task is to find the node with maximum value. Examples: Input: Output: 22 Approach: Just traverse the node from root to right recursively until right is NULL. The node whose right is NULL is the node with the maximum value. Below is the implementation of the above approach: C/C++ Code // C++ implementation of the appr
7 min read
Find the node with maximum value in a Binary Search Tree
Given a Binary Search Tree, the task is to find the node with the maximum value in a BST. For the above tree, we start with 20, then we move right to 22. We keep on moving to the right until we see NULL. Since the right of 22 is NULL, 22 is the node with the maximum value. Approach: This is quite simple. Just traverse the node from root to right re
15 min read
Binary Search Tree vs Ternary Search Tree
For effective searching, insertion, and deletion of items, two types of search trees are used: the binary search tree (BST) and the ternary search tree (TST). Although the two trees share a similar structure, they differ in some significant ways. FeatureBinary Search Tree (BST)Ternary Search Tree (TST)NodeHere, each node has at most two children. H
3 min read
Find the minimum Sub-tree with target sum in a Binary search tree
Given a binary tree and a target, find the number of nodes in the minimum sub-tree with the given sum equal to the target which is also a binary search tree. Examples: Input: 13 / \ 5 23 / \ / \ N 17 N N / 16Target: 38Output: 3Explanation: 5, 17, 16 is the smallest subtree with length 3. Input: 7 / \ N 23 / \ 10 23 / \ / \ N 17 N NTarget: 73Output:
9 min read
Binary Tree to Binary Search Tree Conversion using STL set
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of the Binary Tree.This solution will use Sets of C++ STL instead of array-based solution. Examples: Example 1 Input: 10 / \ 2 7 / \ 8 4 Output: 8 / \ 4 10 / \ 2 7 Example 2 Input: 10 / \ 30 15 / \ 20 5 Output: 15 / \
12 min read
Binary Tree to Binary Search Tree Conversion
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree. Examples Example 1Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10 / \ 2 7Example 2Input: 10 / \ 30 15 / \ 20 5Output: 15 / \ 10 20 / \ 5 30Solution: Following is a 3 step solution for converting Binary tre
15+ min read
Difference between Binary Tree and Binary Search Tree
Binary Tree Data Structure:A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right children. Binary Search Tree Data Structure (BST):A binary search tree is a hierarchical data structure where each node has at most two children, wi
2 min read
Find the Closest Binary Search Tree Value II
Given the root node of a binary search tree (BST) and a target value. The task is to find the k values in the BST that are closest to the target value. The answer can be returned in any order. It is guaranteed that there is only one unique set of k values in the BST that are closest to the target. Example: Input: root = [5,3,6,2,4], target = 3.7142
9 min read
Second unique smallest value of given Binary Tree whose each node is minimum of its children
Given a full binary tree where each node value is the same as the minimum value between its children, the task is to find the second minimum unique value of the tree. Examples: Input: tree: Output: 5Explanation: All the unique values present in the tree are 2, 5 and 7.The second smallest is 5. Input: tree: Output: -1Explanation: All the numbers pre
15+ min read
Search N elements in an unbalanced Binary Search Tree in O(N * logM) time
Given an Unbalanced binary search tree (BST) of M nodes. The task is to find the N elements in the Unbalanced Binary Search Tree in O(N*logM) time. Examples: Input: search[] = {6, 2, 7, 5, 4, 1, 3}. Consider the below tree Output:FoundNot FoundFoundFoundFoundFoundNot Found Naive Approach: For each element, we will try to search for that element in
8 min read
Meta Binary Search | One-Sided Binary Search
Meta binary search (also called one-sided binary search by Steven Skiena in The Algorithm Design Manual on page 134) is a modified form of binary search that incrementally constructs the index of the target value in the array. Like normal binary search, meta binary search takes O(log n) time. Meta Binary Search, also known as One-Sided Binary Searc
9 min read
Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
Given a Binary Search Tree and a binary integer K, the task is to convert Binary search tree into a Skewed Tree in increasing order if K = 0 or in decreasing order if K = 1. Examples: Input: K = 0, 5 / \ 3 6 Output: 3 \ 5 \ 6 Input: K = 1, 2 / \ 1 3 Output: 3 \ 2 \ 1 Approach: The key observation in the problem is that the first node of the skewed
10 min read
Flatten a Binary Search Tree to convert the tree into a wave list in place only
Given a Binary Search Tree consisting of N distinct nodes, the task is to flatten the given Binary Search Tree to convert the tree into a wave list. A wave list arr[0..n-1] is called a wave list if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ... . Examples: Input: 1 / \ 0 10Output: 0 \ 10 \ 1 Input: 3 / \ 1 7 / \ / \ 0 2 5 10 O
12 min read
Difference between Binary Search Tree and AVL Tree
Binary Search Tree:A binary Search Tree is a node-based binary tree data structure that has the following properties: The left subtree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a node contains only nodes with keys greater than the node’s key.The left and right subtree each must also be a binary search t
2 min read
Postorder predecessor of a Node in Binary Search Tree
Given a binary tree and a node in the binary tree, find the Postorder predecessor of the given node. Examples: Consider the following binary tree 20 / \ 10 26 / \ / \ 4 18 24 27 / \ 14 19 / \ 13 15 Input : 4 Output : 10 Postorder traversal of given tree is 4, 13, 15, 14, 19, 18, 10, 24, 27, 26, 20. Input : 24 Output : 10 A simple solution is to fir
8 min read
How to insert a node in Binary Search Tree using Iteration
You are given a binary search tree (BST) and a value to insert into the tree. Print inorder traversal of the BST after the insertion.Example: Input:To the given BST insert 40 Output: Explanation:The new node 40 is a leaf node. Start searching from the root till a leaf node is hit, i.e while searching if a new value is greater than current node move
10 min read
Search a node in Binary Tree
Given a Binary tree and a node. The task is to search and check if the given node exists in the binary tree or not. If it exists, print YES otherwise print NO. Given Binary Tree: Examples: Input: Node = 4 Output: YES Input: Node = 40 Output: NO The idea is to use any of the tree traversals to traverse the tree and while traversing check if the curr
6 min read
Traversal of Binary Search Tree in downward direction from a specific node
Given a Binary Search Tree with unique node values and a target value. Find the node whose data is equal to the target and return all the descendant (of the target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If the target node is not present in bst then return -1. And, if No descendant node is
10 min read
Count the Number of Binary Search Trees present in a Binary Tree
Given a binary tree, the task is to count the number of Binary Search Trees present in it. Examples: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: 4Here each leaf node represents a binary search tree and there are total 4 nodes. Input: 11 / \ 8 10 / / \ 5 9 8 / \ 4 6 Output: 6 Sub-tree rooted under node 5 is a BST 5 / \ 4 6 Another BST we have is rooted
10 min read
Sum and Product of minimum and maximum element of Binary Search Tree
Given a Binary Search Tree. The task is to find the sum and product of the maximum and minimum value of the tree. For the above tree, the sum and product of the maximum and minimum values of the tree are 26 and 88 respectively. Approach: For the node with the minimum value: Find the leftmost leaf nodeFor the node with the maximum value: Find the ri
8 min read
Check whether every node of binary tree has a value K on itself or its any immediate neighbours
Given a binary tree and a value K, the task is to check if every node of the binary tree has either the value of the node as K or at least one of its adjacent connected nodes has the value K. Examples: Input: 1 / \ 0 0 / \ \ 1 0 1 / / \ \ 2 1 0 5 / / 3 0 / 0 K = 0 Output: False Explanation: We can observe that some leaf nodes are having value other
8 min read
Count ancestors with smaller value for each node of a Binary Tree
Given a Binary Tree consisting of N nodes, valued from 1 to N, rooted at node 1, the task is for each node is to count the number of ancestors with a value smaller than that of the current node. Examples: Input: Below is the given Tree: 1 / \ 4 5 / /\ 6 3 2Output: 0 1 1 1 1 2Explanation: Since node 1 is the root node, it has no ancestors.Ancestors
9 min read
Convert a Generic Tree(N-array Tree) to Binary Tree
Prerequisite: Generic Trees(N-array Trees) In this article, we will discuss the conversion of the Generic Tree to a Binary Tree. Following are the rules to convert a Generic(N-array Tree) to a Binary Tree: The root of the Binary Tree is the Root of the Generic Tree.The left child of a node in the Generic Tree is the Left child of that node in the B
13 min read
Distance of each node of a Binary Tree from the root node using BFS
Given a Binary tree consisting of N nodes with values in the range [1, N], the task is to find the distance from the root node to every node of the tree. Examples: Input: 1 / \ 2 3 / \ \ 4 5 6 Output: 0 1 1 2 2 2 Explanation: The distance from the root to node 1 is 0. The distance from the root to node 2 is 1. The distance from the root to node 3 i
9 min read
Step by step Shortest Path from source node to destination node in a Binary Tree
Given a root of binary tree and two integers startValue and destValue denoting the starting and ending node respectively. The task is to find the shortest path from the start node to the end node and print the path in the form of directions given below. Going from one node to its left child node is indicated by the letter 'L'.Going from one node to
14 min read
Print Head node of every node in Binary Tree
Given the root of the Binary Tree. Print the parent node of each node in the given Binary Tree. Examples : Input: Binary Tree = {1, 2, 3, 4, 5, NULL, NULL, NULL, 6} Output : The Head Node of 6 is 4 The Head Nodeof 5 is 2The Head Node of 4 is 2The Head Node of 3 is 1 The Head Node of 2 is 1 Explanation: Clearly By Observing Image we are finding pare
7 min read
Find Next greater element in Binary Search Tree
Given a binary search tree and a target value, the task is to find the next greater element of the target value in the binary search tree. Examples: Input: 5 / \ 3 7 / \ / \ 2 4 6 8 Target: 4Output: 5Explanation: The next greater element of 4 is 5 Input: 4 / \ 2 6 / \ \ 1 3 8 Target: 6Output: 8Explanation: The next greater element of 6 is 8 Approac
12 min read