二叉树的最大深度与最小深度
来源:互联网 发布:java垃圾自动回收 编辑:程序博客网 时间:2024/05/18 03:02
求二叉树的最小深度与最大深度,都是用递归的方法实现。
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.
实现代码:
public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; int left=minDepth(root.left); int right=minDepth(root.right); if(left==0) //如果左子树为空,则返回右子树深度 return right+1; if(right==0) //如果右子树为空,则返回左子树深度 return left+1; return left<right ? left+1:right+1; }}
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
public class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; return (Math.max(maxDepth(root.left)+1, maxDepth(root.right)+1)); }}
0 0
- 二叉树的最大深度与最小深度
- 二叉树的最大深度与最小深度
- leedcode二叉树的最小深度与最大深度
- 二叉树的最大深度与最小深度
- 二叉树的最大深度 & 二叉树的最小深度
- 二叉树的最大深度 & 二叉树的最小深度
- 二叉树的最大最小深度
- 二叉树最大深度和最小深度
- 二叉树最大深度和最小深度
- 二叉树最大 最小深度
- 二叉树 最大最小深度
- 二叉树的最大深度和最小深度
- 二叉树的最大深度和最小深度实现
- 二叉树深度(最大和最小)
- python_lintcode_97二叉树的最大深度 _155二叉树的最小深度
- LeetCode—二叉树的最大最小深度
- 二叉树最大深度最小深度之微妙区别
- 二叉树的最小深度
- Python宝典第十章:系统编程
- HDU 4417 Super Mario--离线树状数组、划分树、线段树
- java遍历实体类的属性和数据类型以及属性值
- 漫谈个性化query推荐
- 客服看价格开始放假看公司开发国家开始非公开身份副食
- 二叉树的最大深度与最小深度
- TCP三次握手连接四次挥手终止连接
- 国际化工和价格和价格就会改好价格和工会级号
- C结构体之位域(位段)_chris
- 家公司开发公开就是客户价格空间设计个看工会的国家队
- 再说combobox——onLoadSuccess用法实例
- 静态导入的作用和实例
- 打印signed 及unsigned限定的char short int 及long类型变量的取值范围
- leetcode - Convert Sorted Array to Binary Search Tree