Leetcode--Minimum Depth of Binary Tree
来源:互联网 发布:php调用python 编辑:程序博客网 时间:2024/06/11 04:37
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.
Tree Depth-first Search思路:记忆深搜获取所有叶子节点的深度,保存到vector中。再将容器的元素排序,结果返回第一个元素
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> res; void function(TreeNode *root,int num) { if(root==NULL) res.push_back(num); else{ int temp=num+1; if(root->left==NULL&&root->right==NULL) function(root->left,temp); else if(root->left!=NULL&&root->right==NULL) function(root->left,temp); else if(root->left==NULL&&root->right!=NULL) function(root->right,temp); else{ function(root->left,temp); function(root->right,temp); } } } int minDepth(TreeNode *root) { if(root==NULL) return 0; function(root,0); sort(res.begin(),res.end()); return res[0]; }};
0 0
- 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
- LeetCode - Minimum Depth of Binary Tree
- 易飞90设计自定义画面新增功能说明
- 算法与数据结构(五)--希尔排序
- 华为荣耀四核官方各版本大集合,速来下载哦!(不定期更新)
- 数挖开源,好像挺不错,未试过
- mac 不能识别android 手机办法
- Leetcode--Minimum Depth of Binary Tree
- tomcat unable to start within 45 seconds
- Android开发:shape和selector和layer-list的(详细说明)
- mac下安装node.js,express,webstorm9
- android自定义listview滑动删除
- iOS之UITextField设置leftView与rightView
- c++ pair
- 通过Jquery判断页面元素是否在浏览器的可视区域内
- codeforces 482A Diverse Permutation 贪心