Question:
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.
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null)
return 0;
return minDepth(root, 0);
}
private int minDepth(TreeNode root, int depth) {
if (root.left == null && root.right == null)
return 1 + depth;
else if (root.left != null && root.right == null)
return minDepth(root.left, depth + 1);
else if (root.left == null && root.right != null)
return minDepth(root.right, depth + 1);
else {
return Math.min(minDepth(root.left, depth + 1), minDepth(root.right, depth + 1));
}
}
}
Test result:
總結:
這道題目太簡單了。。。
如果一定要找出什么總結的地方的話。我覺得就是如何使用遞歸吧崎弃。這里其實用到了一個函數(shù)重載 override 的概念。
題目給的方法是
public int minDepth(TreeNode root)
我自己重載了這個方法
private int minDepth(TreeNode root, int depth)
所以我在公共方法上的代碼可以更加簡單曾沈,而將遞歸過程寫在了我的私有方法上迷扇。
希望女朋友四戰(zhàn)托福可以上90存和!而我的目標是95锡溯!加油!
好久沒刷題了哑姚,希望接下來可以堅持至少每天一道題目。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
return helper(root);
}
private int helper(TreeNode root) {
if (root == null)
return 0;
int left = helper(root.left);
int right = helper(root.right);
if (left == 0)
return right + 1;
else if (right == 0)
return left + 1;
else
return 1 + Math.min(left, right);
}
}
這道題木沒有一遍過芜茵。想簡單的改一下max depth of tree 叙量,取最小值,但是不行九串。
因為如果一個結點的右結點是null的绞佩,那么他就不能算是有深度,不能直接返回0.
而要直接返回左側的深度猪钮。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = minDepth(root.left);
int right = minDepth(root.right);
if (left == 0 && right == 0) {
return 1;
}
else if (left == 0 || right == 0) {
return Math.max(left, right) + 1;
}
else {
return Math.min(left, right) + 1;
}
}
}
題目沒能一遍過品山,還是想簡單了。
在結點上不是去min + 1的烤低。如果左孩子是空肘交,那么返回來的0是不能考慮進去的。
然后寫了下BFS版本:
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
int counter = 0;
while (!q.isEmpty()) {
int size = q.size();
counter++;
for (int i = 0; i < size; i++) {
TreeNode curr = q.poll();
if (curr.left == null && curr.right == null) {
return counter;
}
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
}
return counter;
}
}
Anyway, Good luck, Richardo! -- 09/06/2016