本文共 741 字,大约阅读时间需要 2 分钟。
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.
Java代码:/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int minDepth(TreeNode root) { if(null == root) return 0; if(null ==root.left && null == root.right) return 1; else if(null == root.left) return minDepth(root.right)+1; else if(null == root.right) return minDepth(root.left)+1; else return minDepth(root.left)>minDepth(root.right)?minDepth(root.right)+1:minDepth(root.left)+1; }}
转载地址:http://cnuni.baihongyu.com/