111. Minimum Depth of Binary Tree 发表于 2021-11-13 DFS12345678910111213class Solution { public int minDepth(TreeNode root) { if (root == null) { return 0; } else 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; } }} BFS1234567891011121314151617181920212223242526272829class Solution { public int minDepth(TreeNode root) { int depth = 0; Queue<TreeNode> queue = new LinkedList<>(); if (root != null) { queue.add(root); } while (!queue.isEmpty()) { for (int i = queue.size(); i > 0; i--) { TreeNode node = queue.poll(); if (node.left == null && node.right == null) { return depth + 1; } if (node.left != null) { queue.add(node.left); } if (node.right != null) { queue.add(node.right); } } depth++; } return depth; }} Reference111. Minimum Depth of Binary Tree