...

/

Solution Review: Finding the Height of a Binary Tree

Solution Review: Finding the Height of a Binary Tree

A detailed analysis to help you solve the “Finding the Height of a Binary Tree” challenge.

We'll cover the following...

Solution: Using recursion

Press + to interact
main.cs
BST.cs
using System;
namespace chapter_6
{
class Solution
{
static int findHeight(Node rootNode)
{
if (rootNode == null)
return -1;
else
{
// Find Height of left subtree and then right subtree
// Return greater height value of left or right subtree (plus 1)
int leftHeight = findHeight(rootNode.leftChild);
int rightHeight = findHeight(rootNode.rightChild);
if (leftHeight > rightHeight)
return leftHeight + 1;
else
return rightHeight + 1;
}
}
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree(6);
BST.insertBST(4);
BST.insertBST(9);
BST.insertBST(5);
BST.insertBST(2);
BST.insertBST(8);
BST.insertBST(12);
Console.WriteLine(findHeight(BST.getRoot()));
}
}
}

You ...