Advertisement
Guest User

Untitled

a guest
May 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public int minDepth(TreeNode root) {
  12. if (root == null){
  13. return 0;
  14. } else if(root.left == null) {
  15. return minDepth(root.right) + 1;
  16. } else if(root.right == null) {
  17. return minDepth(root.left) + 1;
  18. }
  19. return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
  20. }
  21. }
  22.  
  23.  
  24. /*
  25. []
  26. [1]
  27. [1,1]
  28. [1,null,2]
  29. [1,2,3,null,null,1,2,1,null,null,null,1]
  30. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement