# [LeetCode]--111. Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

public int minDepth(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
if (minDepth(root.left) == 0)
return minDepth(root.right) + 1;
else if (minDepth(root.right) == 0)
return minDepth(root.left) + 1;
else
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}

public int minDepth1(TreeNode root) {
if (root == null) {
return 0;
}
return getMin(root);
}

public int getMin(TreeNode root) {
if (root == null) {
return Integer.MAX_VALUE;
}

if (root.left == null && root.right == null) {
return 1;
}

return Math.min(getMin(root.left), getMin(root.right)) + 1;
}

public class Solution {
public int minDepth(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
if (root.left == null)
return minDepth(root.right) + 1;
else if (root.right == null)
return minDepth(root.left) + 1;
else
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
}

|
10月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List

35 1
|
10月前
Leetcode Minimum Depth of Binary Tree （面试题推荐）

38 0
|
10月前
Leetcode Binary Tree Postorder Traversal(面试题推荐)

44 0
|
10月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree

30 0
|
10月前
Leetcode 623. Add One Row to Tree

40 0
|

LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
93 0
|

LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
118 0
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
85 0
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III

40 6
|
1月前
|

【Leetcode刷题Python】牛客. 数组中未出现的最小正整数

68 2