标签:blog io ar java for 2014 on log amp
题目:给定一颗二叉树,求出这颗二叉树的最小高度
算法:深度优先算法
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private int minimumDepth = 0x3fffffff; public int minDepth(TreeNode root) { if (root == null) { return 0; } dfs(root, 1); return minimumDepth; } public void dfs(TreeNode node, int sum) { if (node.left==null && node.right==null) { if (sum < minimumDepth) { minimumDepth = sum; } return ; } if (node.left != null) { dfs(node.left, sum+1); } if (node.right != null) { dfs(node.right, sum+1); } } }
标签:blog io ar java for 2014 on log amp
原文地址:http://blog.csdn.net/yeweiouyang/article/details/40351945