Skip to content

Instantly share code, notes, and snippets.

@Roger7410
Created October 12, 2016 21:43
Show Gist options
  • Select an option

  • Save Roger7410/92e2004fa9e3b17ce696c0d073187d7e to your computer and use it in GitHub Desktop.

Select an option

Save Roger7410/92e2004fa9e3b17ce696c0d073187d7e to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int max = 0;
public int count = 0;
public int maxDepth(TreeNode root) {
if(root != null){
count++;
if (root.left == null && root.right == null) {
if(count > max){
max = count;
}
} else {
maxDepth(root.left);
maxDepth(root.right);
}
count--;
}
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment