二叉树的最小深度
来源:互联网 发布:js fetch api 编辑:程序博客网 时间:2024/05/16 17:17
题目
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实现
public int run(TreeNode root) { if (root == null)//空树深度为0 return 0; if (root.left == null && root.right == null)//只有一个节点 return 1; int leftDepth = run(root.left); int rightDepth = run(root.right); if (leftDepth == 0)//左子树深度为0,返回右子树深度加1 return rightDepth + 1; else if (rightDepth == 0) return leftDepth + 1; else return Math.min(leftDepth, rightDepth) + 1; }
阅读全文
0 0
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- BZOJ 3158: 千钧一发 网络流
- 前台开发总结07——20170525
- USACO-Section1.1 Friday the Thirteenth
- boost format用法总结
- 【51Nod】1118 机器人走方格
- 二叉树的最小深度
- 小白项目初尝试第二弹——飞机大战中期
- 关于用公网IP访问不了服务器的问题
- 淘淘商城系列——发布dubbo服务
- linux pppd-GPRS和以太网同时在线,解决拨号默认网关
- Python(5) Image和Ndarray互相转换
- crontab
- linux-linux常用命令汇总
- ShutdownHook