Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created September 27, 2015 19:50
Show Gist options
  • Save danielrobertson/ef1b3be041309b6c879a to your computer and use it in GitHub Desktop.
Save danielrobertson/ef1b3be041309b6c879a to your computer and use it in GitHub Desktop.
Find the maximum depth of a binary tree using depth-first search
/**
* 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