[leetcode]Minimum Depth of Binary Tree
来源:互联网 发布:在淘宝网上怎么退款 编辑:程序博客网 时间:2024/05/01 05:12
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.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int dfs(TreeNode *root){ if(!root-> left && !root->right) return 1; if(root -> left && !root -> right) return dfs(root -> left) + 1; if(!root -> left && root -> right) return dfs(root -> right) + 1; return min(dfs(root->left), dfs(root->right)) + 1; } int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return 0; return dfs(root); }};
- 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
- C++/C 常量
- fork & exec时打开文件的变化
- mysql语句大全
- Ubuntu12.04下安装tslib出现./autogen.sh: 4: ./autogen.sh: autoreconf: not found的错误解决
- 变种异煞
- [leetcode]Minimum Depth of Binary Tree
- 调用iphone联系人
- MySQL DELETE语句和TRUNCATE TABLE语句的区别
- C#中属性的使用方法
- WPF布局
- Ext JS 4 升级指南
- 如何使用手势
- HDU1280 前m大的数
- 【Android LibGDX游戏引擎开发教程】第09期:动画类的使用