A simple solution understood easily


  • 0
    M

    public int maxDepth(TreeNode root) {

         	int deepth=0;
    	 	int left = 0,right = 0;
            
            if(root!=null && root.left == null && root.right == null){
            	
                return 1;
            }
            else{
        	   
        	   if(root!=null &&root.left != null){
        		   
        		   left =  maxDepth(root.left) + 1;
        	   }
        	   if(root!=null &&root.right != null){
        		   
        		   right =  maxDepth(root.right) + 1;
        	   }
        	   
        	   deepth = (left>=right)?left:right;
           }
           return deepth ;
           
    }

  • -2
    J
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.