Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yohey03518/c58a3911019bc83edb040ea1aaef2f7e to your computer and use it in GitHub Desktop.
Save yohey03518/c58a3911019bc83edb040ea1aaef2f7e to your computer and use it in GitHub Desktop.
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