Minimum Depth of Binary Tree LeetCode solution in Java

1
/**
2
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
3
* right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left,
4
* TreeNode right) { this.val = val; this.left = left; this.right = right; } }
5
*/
6
class Solution {
7
public int minDepth(TreeNode root) {
8
if (root == null) return 0;
9

10
int left = minDepth(root.left);
11
int right = minDepth(root.right);
12
if (root.left == null) return right + 1;
13
if (root.right == null) return left + 1;
14
return Math.min(left, right) + 1;
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0