Created
September 10, 2016 12:29
-
-
Save leearmee35/86c137b19d3e3b2c239b44bc923a214b to your computer and use it in GitHub Desktop.
This file contains 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 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