Given a Binary tree, print the maximum depth of a left node.( the node needs to be a left child ) (if the node is right child of the left child of the root node then it wont count as a left node)
import java.io.*;
class Node
{
int data;
Node left,right;
Node(int data)
{
this.data=data;
left=right=null;
}
}
class TreeQuestion {
Node root;
static int max=0;
void maxDepth(Node root,int direction,int depth)
{
if(root==null)
return;
if(direction==1)
{
max=Math.max(max,depth);
}
maxDepth(root.left,1,depth+1);
maxDepth(root.right,2,depth+1);
}
public static void main (String[] args) {
TreeQuestion tree=new TreeQuestion();
tree.root=new Node(1);
tree.root.left=new Node(2);
tree.root.left.left=new Node(3);
tree.root.left.left.left=new Node(4);
tree.maxDepth(tree.root,1,0);
System.out.println(max+1);
}
}
import java.io.*;
class Node
{
int data;
Node left,right;
Node(int data)
{
this.data=data;
left=right=null;
}
}
class TreeQuestion {
Node root;
static int max=0;
void maxDepth(Node root,int direction,int depth)
{
if(root==null)
return;
if(direction==1)
{
max=Math.max(max,depth);
}
maxDepth(root.left,1,depth+1);
maxDepth(root.right,2,depth+1);
}
public static void main (String[] args) {
TreeQuestion tree=new TreeQuestion();
tree.root=new Node(1);
tree.root.left=new Node(2);
tree.root.left.left=new Node(3);
tree.root.left.left.left=new Node(4);
tree.maxDepth(tree.root,1,0);
System.out.println(max+1);
}
}