LeetCode 41 Minimum Depth of Binary Tree
来源:互联网 发布:淘宝的宝贝详情在哪 编辑:程序博客网 时间:2024/06/04 18:25
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 nearest leaf node.
分析:
二叉树用递归。
/** * 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(root==null) return 0; int lMin = minDepth(root.left); int rMin = minDepth(root.right); if(lMin==0 && rMin==0) return 1; //下面这两行是避免把只有一个孩子的中间节点当成叶子节点 if(lMin == 0) lMin = Integer.MAX_VALUE; if(rMin == 0) rMin = Integer.MAX_VALUE; return Math.min(lMin, rMin) + 1; }}
0 0
- LeetCode 41 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 - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- 【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-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
- 学习笔记2——C语言程序的组成部分和C语言的文件流
- The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
- 第一章--绪论
- http://www.coder4.com/
- hdu 12490 Round Robin
- LeetCode 41 Minimum Depth of Binary Tree
- Codeforce 264 A Caisa and Sugar(简单)
- 负载均衡
- 用fildder 查看loveuv 刷流量时通信的数据
- Codeforces Round 264(div2) C. Gargari and Bishops
- Ubuntu下用glade和GTK+开发C语言界面程序(二)——gcc常用命令介绍
- 外传:电脑网络连接显示正常,但是不能上网?
- TortoiseGit Disconnected: No supported authentication methods available (server sent: publickey)
- 回文字符串