Get level of a node in a binary tree

Here given code implementation process.

/*
    C Program 
    Get level of a node in a binary tree
*/
#include <stdio.h>

#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *newTree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// returns a new node of tree
struct TreeNode *newNode(int data)
{
	// Create dynamic node
	struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (node != NULL)
	{
		//Set data and pointer values
		node->data = data;
		node->left = NULL;
		node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return node;
}
// Returns the level of given key node
// In case node key not found then returns 0
int findLevel(struct TreeNode *node, int level, int nodeKey)
{
	if (node == NULL)
	{
		return 0;
	}
	if (nodeKey == node->data)
	{
		return level;
	}
	// Reversibly, visit left 
	int ans = findLevel(node->left, level + 1, nodeKey);
	if (ans == 0)
	{
		// When result not found in left subtree
		// Reversibly, visit right 
		ans = findLevel(node->right, level + 1, nodeKey);
	}
	return ans;
}
// Handles the request to find the given node key level
void findeNodeLevel(struct TreeNode *root, int nodeKey)
{
	if (root == NULL)
	{
		printf("\n Empty Tree \n");
		return;
	}
	int ans = findLevel(root, 1, nodeKey);
	if (ans != 0)
	{
		printf("\n Node key %d exist at level : %d", nodeKey, ans);
	}
	else
	{
		printf("\n Node key %d not found ", nodeKey);
	}
}
int main()
{
	// Define tree
	struct BinaryTree *tree = newTree();
	/*
	      1
	    /  \
	   /    \
	  2      8
	 / \    / \
	3  10  6   4
	   /  / \   
	  7  5   9
	   
	-----------------------
	   Binary Tree
	-----------------------
	*/
	tree->root = newNode(1);
	tree->root->left = newNode(2);
	tree->root->right = newNode(8);
	tree->root->left->left = newNode(3);
	tree->root->left->right = newNode(10);
	tree->root->left->right->left = newNode(7);
	tree->root->right->left = newNode(6);
	tree->root->right->left->right = newNode(9);
	tree->root->right->right = newNode(4);
	tree->root->right->left->left = newNode(5);
	// Test
	findeNodeLevel(tree->root, 5);
	findeNodeLevel(tree->root, 2);
	findeNodeLevel(tree->root, 11);
	return 0;
}

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
/*
  Java Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	public int findLevel(TreeNode node, int level, int nodeKey)
	{
		if (node == null)
		{
			return 0;
		}
		if (nodeKey == node.data)
		{
			return level;
		}
		// Reversibly, visit left 
		int ans = findLevel(node.left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right 
			ans = findLevel(node.right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	public void findeNodeLevel(int nodeKey)
	{
		if (this.root == null)
		{
			System.out.print("\n Empty Tree \n");
			return;
		}
		int ans = findLevel(this.root, 1, nodeKey);
		if (ans != 0)
		{
			System.out.print("\n Node key " + nodeKey + " exist at level : " + ans);
		}
		else
		{
			System.out.print("\n Node key " + nodeKey + " not found ");
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
              1
            /  \
           /    \
          2      8
         / \    / \
        3  10  6   4
           /  / \   
          7  5   9
           
        -----------------------
           Binary Tree
        -----------------------
        */
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(10);
		tree.root.left.right.left = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.left.right = new TreeNode(9);
		tree.root.right.right = new TreeNode(4);
		tree.root.right.left.left = new TreeNode(5);
		// Test
		tree.findeNodeLevel(5);
		tree.findeNodeLevel(2);
		tree.findeNodeLevel(11);
	}
}

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode
{
	public: int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	int findLevel(TreeNode *node, int level, int nodeKey)
	{
		if (node == NULL)
		{
			return 0;
		}
		if (nodeKey == node->data)
		{
			return level;
		}
		// Reversibly, visit left
		int ans = this->findLevel(node->left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			ans = this->findLevel(node->right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	void findeNodeLevel(int nodeKey)
	{
		if (this->root == NULL)
		{
			cout << "\n Empty Tree \n";
			return;
		}
		int ans = this->findLevel(this->root, 1, nodeKey);
		if (ans != 0)
		{
			cout << "\n Node key " << nodeKey << " exist at level : " << ans;
		}
		else
		{
			cout << "\n Node key " << nodeKey << " not found ";
		}
	}
};
int main()
{
	BinaryTree tree = BinaryTree();
	/*
	              1
	            /  \
	           /    \
	          2      8
	         / \    / \
	        3  10  6   4
	           /  / \   
	          7  5   9
	           
	        -----------------------
	           Binary Tree
	        -----------------------
	        
	*/
	tree.root = new TreeNode(1);
	tree.root->left = new TreeNode(2);
	tree.root->right = new TreeNode(8);
	tree.root->left->left = new TreeNode(3);
	tree.root->left->right = new TreeNode(10);
	tree.root->left->right->left = new TreeNode(7);
	tree.root->right->left = new TreeNode(6);
	tree.root->right->left->right = new TreeNode(9);
	tree.root->right->right = new TreeNode(4);
	tree.root->right->left->left = new TreeNode(5);
	// Test
	tree.findeNodeLevel(5);
	tree.findeNodeLevel(2);
	tree.findeNodeLevel(11);
	return 0;
}

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
// Include namespace system
using System;
/*
  C# Program
  Get level of a node in a binary tree
*/
// Tree Node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	public int findLevel(TreeNode node, int level, int nodeKey)
	{
		if (node == null)
		{
			return 0;
		}
		if (nodeKey == node.data)
		{
			return level;
		}
		// Reversibly, visit left
		int ans = findLevel(node.left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			ans = findLevel(node.right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	public void findeNodeLevel(int nodeKey)
	{
		if (this.root == null)
		{
			Console.Write("\n Empty Tree \n");
			return;
		}
		int ans = findLevel(this.root, 1, nodeKey);
		if (ans != 0)
		{
			Console.Write("\n Node key " + nodeKey + " exist at level : " + ans);
		}
		else
		{
			Console.Write("\n Node key " + nodeKey + " not found ");
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		          1
		        /  \
		       /    \
		      2      8
		     / \    / \
		    3  10  6   4
		       /  / \   
		      7  5   9
		       
		    -----------------------
		       Binary Tree
		    -----------------------
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(10);
		tree.root.left.right.left = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.left.right = new TreeNode(9);
		tree.root.right.right = new TreeNode(4);
		tree.root.right.left.left = new TreeNode(5);
		// Test
		tree.findeNodeLevel(5);
		tree.findeNodeLevel(2);
		tree.findeNodeLevel(11);
	}
}

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
<?php
/*
  Php Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		$this->root = null;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	public	function findLevel($node, $level, $nodeKey)
	{
		if ($node == null)
		{
			return 0;
		}
		if ($nodeKey == $node->data)
		{
			return $level;
		}
		// Reversibly, visit left
		$ans = $this->findLevel($node->left, $level + 1, $nodeKey);
		if ($ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			$ans = $this->findLevel($node->right, $level + 1, $nodeKey);
		}
		return $ans;
	}
	// Handles the request to find the given node key level
	public	function findeNodeLevel($nodeKey)
	{
		if ($this->root == null)
		{
			echo "\n Empty Tree \n";
			return;
		}
		$ans = $this->findLevel($this->root, 1, $nodeKey);
		if ($ans != 0)
		{
			echo "\n Node key ". $nodeKey ." exist at level : ". $ans;
		}
		else
		{
			echo "\n Node key ". $nodeKey ." not found ";
		}
	}
}

function main()
{
	$tree = new BinaryTree();
	/*
	          1
	        /  \
	       /    \
	      2      8
	     / \    / \
	    3  10  6   4
	       /  / \   
	      7  5   9
	       
	    -----------------------
	       Binary Tree
	    -----------------------
	*/
	$tree->root = new TreeNode(1);
	$tree->root->left = new TreeNode(2);
	$tree->root->right = new TreeNode(8);
	$tree->root->left->left = new TreeNode(3);
	$tree->root->left->right = new TreeNode(10);
	$tree->root->left->right->left = new TreeNode(7);
	$tree->root->right->left = new TreeNode(6);
	$tree->root->right->left->right = new TreeNode(9);
	$tree->root->right->right = new TreeNode(4);
	$tree->root->right->left->left = new TreeNode(5);
	// Test
	$tree->findeNodeLevel(5);
	$tree->findeNodeLevel(2);
	$tree->findeNodeLevel(11);
}
main();

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
/*
  Node Js Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode
{
	constructor(data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	findLevel(node, level, nodeKey)
	{
		if (node == null)
		{
			return 0;
		}
		if (nodeKey == node.data)
		{
			return level;
		}
		// Reversibly, visit left
		var ans = this.findLevel(node.left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			ans = this.findLevel(node.right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	findeNodeLevel(nodeKey)
	{
		if (this.root == null)
		{
			process.stdout.write("\n Empty Tree \n");
			return;
		}
		var ans = this.findLevel(this.root, 1, nodeKey);
		if (ans != 0)
		{
			process.stdout.write("\n Node key " + nodeKey + " exist at level : " + ans);
		}
		else
		{
			process.stdout.write("\n Node key " + nodeKey + " not found ");
		}
	}
}

function main()
{
	var tree = new BinaryTree();
	/*
	          1
	        /  \
	       /    \
	      2      8
	     / \    / \
	    3  10  6   4
	       /  / \   
	      7  5   9
	       
	    -----------------------
	       Binary Tree
	    -----------------------
	*/
	tree.root = new TreeNode(1);
	tree.root.left = new TreeNode(2);
	tree.root.right = new TreeNode(8);
	tree.root.left.left = new TreeNode(3);
	tree.root.left.right = new TreeNode(10);
	tree.root.left.right.left = new TreeNode(7);
	tree.root.right.left = new TreeNode(6);
	tree.root.right.left.right = new TreeNode(9);
	tree.root.right.right = new TreeNode(4);
	tree.root.right.left.left = new TreeNode(5);
	// Test
	tree.findeNodeLevel(5);
	tree.findeNodeLevel(2);
	tree.findeNodeLevel(11);
}
main();

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
#   Python 3 Program
#   Get level of a node in a binary tree

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

class BinaryTree :
	
	def __init__(self) :
		self.root = None
	
	#  Returns the level of given key node
	#  In case node key not found then returns 0
	def findLevel(self, node, level, nodeKey) :
		if (node == None) :
			return 0
		
		if (nodeKey == node.data) :
			return level
		
		#  Reversibly, visit left
		ans = self.findLevel(node.left, level + 1, nodeKey)
		if (ans == 0) :
			#  When result not found in left subtree
			#  Reversibly, visit right
			ans = self.findLevel(node.right, level + 1, nodeKey)
		
		return ans
	
	#  Handles the request to find the given node key level
	def findeNodeLevel(self, nodeKey) :
		if (self.root == None) :
			print("\n Empty Tree ")
			return
		
		ans = self.findLevel(self.root, 1, nodeKey)
		if (ans != 0) :
			print("\n Node key ", nodeKey ," exist at level : ", ans, end = "")
		else :
			print("\n Node key ", nodeKey ," not found ", end = "")
		
	

def main() :
	tree = BinaryTree()
	# 
	#           1
	#         /  \
	#        /    \
	#       2      8
	#      / \    / \
	#     3  10  6   4
	#        /  / \   
	#       7  5   9
	#        
	#     -----------------------
	#        Binary Tree
	#     -----------------------
	
	tree.root = TreeNode(1)
	tree.root.left = TreeNode(2)
	tree.root.right = TreeNode(8)
	tree.root.left.left = TreeNode(3)
	tree.root.left.right = TreeNode(10)
	tree.root.left.right.left = TreeNode(7)
	tree.root.right.left = TreeNode(6)
	tree.root.right.left.right = TreeNode(9)
	tree.root.right.right = TreeNode(4)
	tree.root.right.left.left = TreeNode(5)
	#  Test
	tree.findeNodeLevel(5)
	tree.findeNodeLevel(2)
	tree.findeNodeLevel(11)

if __name__ == "__main__": main()

Output

 Node key  5  exist at level :  4
 Node key  2  exist at level :  2
 Node key  11  not found
#   Ruby Program
#   Deepest left leaf node in a binary tree

#  Tree Node
class TreeNode  
	# Define the accessor and reader of class TreeNode  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
 
	
	def initialize(data) 
		self.data = data
		self.left = nil
		self.right = nil
	end

end

class BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root
	attr_accessor :root
 
	
	def initialize() 
		self.root = nil
	end

	#  Returns the level of given key node
	#  In case node key not found then returns 0
	def findLevel(node, level, nodeKey) 
		if (node == nil) 
			return 0
		end

		if (nodeKey == node.data) 
			return level
		end

		#  Reversibly, visit left
		ans = self.findLevel(node.left, level + 1, nodeKey)
		if (ans == 0) 
			#  When result not found in left subtree
			#  Reversibly, visit right
			ans = self.findLevel(node.right, level + 1, nodeKey)
		end

		return ans
	end

	#  Handles the request to find the given node key level
	def findeNodeLevel(nodeKey) 
		if (self.root == nil) 
			print("\n Empty Tree \n")
			return
		end

		ans = self.findLevel(self.root, 1, nodeKey)
		if (ans != 0) 
			print("\n Node key ", nodeKey ," exist at level : ", ans)
		else 
			print("\n Node key ", nodeKey ," not found ")
		end

	end

end

def main() 
	tree = BinaryTree.new()
	# 
	#           1
	#         /  \
	#        /    \
	#       2      8
	#      / \    / \
	#     3  10  6   4
	#        /  / \   
	#       7  5   9
	#        
	#     -----------------------
	#        Binary Tree
	#     -----------------------
	
	tree.root = TreeNode.new(1)
	tree.root.left = TreeNode.new(2)
	tree.root.right = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(3)
	tree.root.left.right = TreeNode.new(10)
	tree.root.left.right.left = TreeNode.new(7)
	tree.root.right.left = TreeNode.new(6)
	tree.root.right.left.right = TreeNode.new(9)
	tree.root.right.right = TreeNode.new(4)
	tree.root.right.left.left = TreeNode.new(5)
	#  Test
	tree.findeNodeLevel(5)
	tree.findeNodeLevel(2)
	tree.findeNodeLevel(11)
end

main()

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found 
/*
  Scala Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	def findLevel(node: TreeNode, level: Int, nodeKey: Int): Int = {
		if (node == null)
		{
			return 0;
		}
		if (nodeKey == node.data)
		{
			return level;
		}
		// Reversibly, visit left
		var ans: Int = this.findLevel(node.left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			ans = this.findLevel(node.right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	def findeNodeLevel(nodeKey: Int): Unit = {
		if (this.root == null)
		{
			print("\n Empty Tree \n");
			return;
		}
		var ans: Int = this.findLevel(this.root, 1, nodeKey);
		if (ans != 0)
		{
			print("\n Node key " + nodeKey + " exist at level : " + ans);
		}
		else
		{
			print("\n Node key " + nodeKey + " not found ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree: BinaryTree = new BinaryTree();
		/*
		          1
		        /  \
		       /    \
		      2      8
		     / \    / \
		    3  10  6   4
		       /  / \   
		      7  5   9
		       
		    -----------------------
		       Binary Tree
		    -----------------------
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(10);
		tree.root.left.right.left = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.left.right = new TreeNode(9);
		tree.root.right.right = new TreeNode(4);
		tree.root.right.left.left = new TreeNode(5);
		// Test
		tree.findeNodeLevel(5);
		tree.findeNodeLevel(2);
		tree.findeNodeLevel(11);
	}
}

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found
/*
  Swift 4 Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	func findLevel(_ node: TreeNode? , _ level : Int, _ nodeKey: Int)->Int
	{
		if (node == nil)
		{
			return 0;
		}
		if (nodeKey == node!.data)
		{
			return level;
		}
		// Reversibly, visit left
		var ans: Int = self.findLevel(node!.left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			ans = self.findLevel(node!.right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	func findeNodeLevel(_ nodeKey: Int)
	{
		if (self.root == nil)
		{
			print("\n Empty Tree ");
			return;
		}
		let ans: Int = self.findLevel(self.root, 1, nodeKey);
		if (ans  != 0)
		{
			print("\n Node key ", nodeKey ," exist at level : ", ans, terminator: "");
		}
		else
		{
			print("\n Node key ", nodeKey ," not found ", terminator: "");
		}
	}
}
func main()
{
	let tree: BinaryTree = BinaryTree();
	/*
	          1
	        /  \
	       /    \
	      2      8
	     / \    / \
	    3  10  6   4
	       /  / \   
	      7  5   9
	       
	    -----------------------
	       Binary Tree
	    -----------------------
	*/
	tree.root = TreeNode(1);
	tree.root!.left = TreeNode(2);
	tree.root!.right = TreeNode(8);
	tree.root!.left!.left = TreeNode(3);
	tree.root!.left!.right = TreeNode(10);
	tree.root!.left!.right!.left = TreeNode(7);
	tree.root!.right!.left = TreeNode(6);
	tree.root!.right!.left!.right = TreeNode(9);
	tree.root!.right!.right = TreeNode(4);
	tree.root!.right!.left!.left = TreeNode(5);
	// Test
	tree.findeNodeLevel(5);
	tree.findeNodeLevel(2);
	tree.findeNodeLevel(11);
}
main();

Output

 Node key  5  exist at level :  4
 Node key  2  exist at level :  2
 Node key  11  not found
/*
  Kotlin Program
  Get level of a node in a binary tree
*/
// Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// Returns the level of given key node
	// In case node key not found then returns 0
	fun findLevel(node: TreeNode ? , level : Int, nodeKey: Int): Int
	{
		if (node == null)
		{
			return 0;
		}
		if (nodeKey == node.data)
		{
			return level;
		}
		// Reversibly, visit left
		var ans: Int = this.findLevel(node.left, level + 1, nodeKey);
		if (ans == 0)
		{
			// When result not found in left subtree
			// Reversibly, visit right
			ans = this.findLevel(node.right, level + 1, nodeKey);
		}
		return ans;
	}
	// Handles the request to find the given node key level
	fun findeNodeLevel(nodeKey: Int): Unit
	{
		if (this.root == null)
		{
			print("\n Empty Tree \n");
			return;
		}
		var ans: Int = this.findLevel(this.root, 1, nodeKey);
		if (ans != 0)
		{
			print("\n Node key " + nodeKey + " exist at level : " + ans);
		}
		else
		{
			print("\n Node key " + nodeKey + " not found ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var tree: BinaryTree = BinaryTree();
	/*
	          1
	        /  \
	       /    \
	      2      8
	     / \    / \
	    3  10  6   4
	       /  / \   
	      7  5   9
	       
	    -----------------------
	       Binary Tree
	    -----------------------
	*/
	tree.root = TreeNode(1);
	tree.root?.left = TreeNode(2);
	tree.root?.right = TreeNode(8);
	tree.root?.left?.left = TreeNode(3);
	tree.root?.left?.right = TreeNode(10);
	tree.root?.left?.right?.left = TreeNode(7);
	tree.root?.right?.left = TreeNode(6);
	tree.root?.right?.left?.right = TreeNode(9);
	tree.root?.right?.right = TreeNode(4);
	tree.root?.right?.left?.left = TreeNode(5);
	// Test
	tree.findeNodeLevel(5);
	tree.findeNodeLevel(2);
	tree.findeNodeLevel(11);
}

Output

 Node key 5 exist at level : 4
 Node key 2 exist at level : 2
 Node key 11 not found


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved