Created
August 17, 2018 15:20
-
-
Save yohey03518/c58a3911019bc83edb040ea1aaef2f7e to your computer and use it in GitHub Desktop.
This file contains 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
public int MaxDepth(TreeNode root) | |
{ | |
if (root == null) | |
return 0; | |
if (root.left == null && root.right == null) | |
return 1; | |
int maxDepthLeft = 0, maxDepthRight = 0; | |
if (root.left != null) | |
maxDepthLeft = 1 + MaxDepth(root.left); | |
if (root.right != null) | |
maxDepthRight = 1 + MaxDepth(root.right); | |
if (maxDepthLeft > maxDepthRight) | |
return maxDepthLeft; | |
else | |
return maxDepthRight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment