Created
September 27, 2015 19:50
-
-
Save danielrobertson/ef1b3be041309b6c879a to your computer and use it in GitHub Desktop.
Find the maximum depth of a binary tree using depth-first search
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
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public int maxDepth(TreeNode root) { | |
int depth = 0; | |
if(root == null) | |
return depth; | |
int lDepth = maxDepth(root.left); | |
int rDepth = maxDepth(root.right); | |
return 1 + Math.max(lDepth, rDepth); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment