[LeetCode]Minimum Depth of Binary Tree
2014-10-21 21:26
281 查看
题目:给定一颗二叉树,求出这颗二叉树的最小高度
算法:深度优先算法
算法:深度优先算法
/** * 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); } } }
相关文章推荐
- [面试真题] LeetCode:Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [leetcode刷题系列]Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree
- 【leetcode】Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- leetcode--Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- LeetCode-Minimum Depth of Binary Tree
- 【LeetCode with Python】 Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- Leetcode: Minimum Depth of Binary Tree
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- leetcode: Minimum Depth of Binary Tree
- 【leetcode】Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- LeetCode_Minimum Depth of Binary Tree
- LeetCode题目笔记(一) -- Minimum Depth Of Binary Tree
- leetcode -- Minimum Depth of Binary Tree