Skip to content

Instantly share code, notes, and snippets.

@leearmee35
Created September 10, 2016 12:29
Show Gist options
  • Save leearmee35/86c137b19d3e3b2c239b44bc923a214b to your computer and use it in GitHub Desktop.
Save leearmee35/86c137b19d3e3b2c239b44bc923a214b 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 maxDepth(TreeNode root) {
int left = 1;
int right = 1;
if(root==null){
return 0;
}
if(root.left==null && root.right==null){
return 1;
}
if(root.left!=null){
left+=maxDepth(root.left);
System.out.println(left);
}
if(root.right!=null){
right+=maxDepth(root.right);
System.out.println(right);
}
if(left>right)
{
return left;
}
return right;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment