Advertisement
Guest User

ugly

a guest
Jan 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. public int maxDepth(TreeNode root){
  2. if(root == null){
  3. return 0;
  4. }
  5. else if(root.left == null && root.right == null){
  6. return 0;
  7. }
  8. else{
  9. return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
  10. }
  11. }
  12.  
  13. public int diameterOfBinaryTree(TreeNode root) {
  14. if(root == null){
  15. return 0;
  16. }
  17. else if(root.left == null && root.right == null){
  18. return 0;
  19. }
  20. int leftPath = root.left == null ? 0 : 1 + maxDepth(root.left);
  21. int rightPath = root.right == null ? 0 : 1 + maxDepth(root.right);
  22. return Math.max(Math.max(leftPath + rightPath, diameterOfBinaryTree(root.left)), diameterOfBinaryTree(root.right));
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement