Find the Deepest Node in a Binary Tree | A C # program to find value of the deepest node in a given binary tree ; A tree node with constructor ; constructor ; Funtion to return the deepest node ; Creating a Queue ; Iterates untill queue become empty ; Driver code
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int key ) { data = key ; left = null ; right = null ; } } ; class Gfg { public static Node deepestNode ( Node root ) { Node tmp = null ; if ( root == null ) return null ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { tmp = q . Peek ( ) ; q . Dequeue ( ) ; if ( tmp . left != null ) q . Enqueue ( tmp . left ) ; if ( tmp . right != null ) q . Enqueue ( tmp . right ) ; } return tmp ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; Node deepNode = deepestNode ( root ) ; Console . WriteLine ( deepNode . data ) ; } }