leetcode 111:Minimum Depth of Binary Tree
来源:互联网 发布:b超图片上的数据 编辑:程序博客网 时间:2024/06/05 14:15
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.
class Solution {public: int minDepth(TreeNode* root) { int ret=1000000; if(!root) return 0; help(root,1,ret); return ret; }private: void help(TreeNode* root,int depth,int& ret) { if(!root->left&&!root->right) ret=min(depth,ret); else if(root->left&&root->right) { help(root->left,depth+1,ret); help(root->right,depth+1,ret); } else if(root->right&&!root->left) help(root->right,depth+1,ret); else help(root->left,depth+1,ret); } int min(int a,int b) { return a<b?a:b; }};
呵呵哒,老子很失落!
学习下大佬的代码:BFS
int minDepth(TreeNode* root) { if (root == NULL) return 0; queue<TreeNode*> Q; Q.push(root); int i = 0; while (!Q.empty()) { i++; int k = Q.size(); for (int j=0; j<k; j++) { TreeNode* rt = Q.front(); if (rt->left) Q.push(rt->left); if (rt->right) Q.push(rt->right); Q.pop(); if (rt->left==NULL && rt->right==NULL) return i; } } return -1; //For the compiler thing. The code never runs here.}
阅读全文
0 0
- LeetCode(111)Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree [111]
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- [LeetCode 111]Minimum Depth of Binary Tree
- leetCode #111 Minimum Depth of Binary Tree
- leetcode-111 Minimum Depth of Binary Tree
- leetcode 111 Minimum Depth of Binary Tree
- leetcode || 111、Minimum Depth of Binary Tree
- Leetcode[111]-Minimum Depth of Binary Tree
- leetcode[111]:Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- leetcode-111-Minimum Depth of Binary Tree
- Leetcode #111 Minimum Depth of Binary Tree
- leetcode 111: Minimum Depth of Binary Tree
- LeetCode 111: Minimum Depth of Binary Tree
- 【LEETCODE】111-Minimum Depth of Binary Tree
- 信息化系统工程IPTV或OTT-TV节目系统解决方案
- 集合
- bashrc profile
- C#错误异常日志记录到文件
- 一个文件实现安卓滚轮选择控件
- leetcode 111:Minimum Depth of Binary Tree
- VSCode 快捷键 Windows版
- Android 用webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决 .
- Linux进程管理
- Android Studio:Not a PNG file
- CentOS虚拟机NAT方式无法上网
- RSA签名和验签过程
- es6 babel转码器安装配置
- 什么是低耗蓝牙BLE以及与经典蓝牙的优劣