﻿ C# exercises and solutions-max min nodes of BS tree

## C# OOP: Binary Search Tree

### Max and Min nodes of BS tree

Step 3: Finding the min and max nodes

In binary search tree, the smallest node is in the left side and the largest node is in the right side.

To find the smallest node, the process will check the parent node. In case that the parent node is not empty, if it doesn't a left child node, the smallest node is the parent node, otherwise the smallest node is its left child node.

//Find the min node

public TreeNode findmin(TreeNode Tree){
if(Tree==null) return null;
else if(Tree.left==null) return Tree;
else return findmin(Tree.left);
}

To find the largest node, the process will check the parent node. In case that the parent node is not empty, if it doesn't a right child node, the largest node is the parent node, otherwise the largest node is its right child node.

//Find the max node

public TreeNode findmax(TreeNode Tree){
if(Tree==null) return null;
else if(Tree.right==null) return Tree;
else return findmax(Tree.right);
}