Minimum Depth of Binary Tree
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.
思路:
dfs
我的代码:
public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null && root.right == null) return 1; int left = Integer.MAX_VALUE; int right = Integer.MAX_VALUE; if(root.left != null) left = minDepth(root.left); if(root.right != null) right = minDepth(root.right); return Math.min(left, right) + 1; } }
posted on 2015-03-14 16:09 zhouzhou0615 阅读(155) 评论(0) 编辑 收藏 举报