Created
May 4, 2024 10:44
-
-
Save tewari2312/903a23b776479f1e4651b7ca004f74f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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