Advertisement
CreateWithChirag

Maximum Depth of Binary Tree

Mar 7th, 2023
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | Source Code | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode() {}
  8.  *     TreeNode(int val) { this.val = val; }
  9.  *     TreeNode(int val, TreeNode left, TreeNode right) {
  10.  *         this.val = val;
  11.  *         this.left = left;
  12.  *         this.right = right;
  13.  *     }
  14.  * }
  15.  */
  16. class Solution {
  17.     public int maxDepth(TreeNode root) {
  18.         if(root == null){
  19.             return 0;
  20.         }
  21.  
  22.         int lh = maxDepth(root.left);
  23.         int rh = maxDepth(root.right);
  24.  
  25.         return 1 + Math.max(lh,rh);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement