Skip to content

Instantly share code, notes, and snippets.

@tewari2312
Created May 4, 2024 10:44
Show Gist options
  • Select an option

  • Save tewari2312/903a23b776479f1e4651b7ca004f74f7 to your computer and use it in GitHub Desktop.

Select an option

Save tewari2312/903a23b776479f1e4651b7ca004f74f7 to your computer and use it in GitHub Desktop.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class MaxDepthOfBinaryTree {
int maxDepth = 0;
int currentDepth = 0;
public int maxDepth(TreeNode root) {
if(root!=null){
currentDepth++;
maxDepth = currentDepth>maxDepth?currentDepth:maxDepth;
if(root.left!=null)
maxDepth(root.left);
if(root.right!=null)
maxDepth(root.right);
currentDepth--;
}
return maxDepth;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment