111. Minimum Depth of Binary Tree
来源:互联网 发布:js如何设置div的高 编辑:程序博客网 时间:2024/06/05 21:04
寻找树的最小路径长度
方法1:深搜
const int Max=1000000;class Solution {public: void DFS(TreeNode* root,int depth,int &min) { if(root==NULL) return; if(root->left==NULL&&root->right==NULL) { depth++; if(min>depth) min=depth; return; } depth++; DFS(root->left,depth,min); DFS(root->right, depth, min); } int minDepth(TreeNode* root) { if(root==NULL) return 0; int min=Max; int depth=0; DFS(root,depth,min); return min; } };
方法2:分治
class Solution {public: int minDepth(TreeNode* root) { if(!root) return 0; if(!root->left) return minDepth(root->right)+1; if(!root->right) return minDepth(root->left)+1; int l=minDepth(root->left); int r=minDepth(root->right); return min(l,r)+1; } };
0 0
- 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
- 111.Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【LeetCode】111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【UVa】208 - Firetruck
- C与C++注释转换
- 数据结构实验之二叉树六:哈夫曼编码
- 2-10 霍纳法则(秦九韶算法)--求多项式
- G470 deepin wifi无法开启
- 111. Minimum Depth of Binary Tree
- linux中rm命令使用详解
- 【UVa】12325 - Zombie's Treasure Chest
- c# 数据统计(将Datagridview的列数据合计)
- 【UVa】524 - Prime Ring Problem
- 第一行代码,Activity部分
- 使用Glide动态加载圆形图片和圆角图片
- Qt5设置应用程序图标
- xcode 7 打包静态库 .a 文件