《leetCode》:Minimum Depth of Binary Tree
来源:互联网 发布:企业开淘宝网店交税 编辑:程序博客网 时间:2024/06/07 22:03
题目
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 a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int min(int a,int b){ return (a<=b)?a:b;}int minDepth(struct TreeNode* root) { if(root==NULL){ return 0; } if(root->left==NULL&&root->right==NULL){ return 1; } else if(root->left==NULL){ return minDepth(root->right)+1; } else if(root->right==NULL){ return minDepth(root->left)+1; } else return min(minDepth(root->left),minDepth(root->right))+1; }
1 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
- 对List数组进行分页显示
- Java 时区之间时间转换
- 【项目构建工具】——Maven知多少
- Oracle_字符集问题(数据库与客户端字符集关联关系)
- OpenAL源码编译
- 《leetCode》:Minimum Depth of Binary Tree
- HTML下拉框多选
- 七个Swift中的陷阱以及避免方法
- 【ZOJ-2588】Burning Bridges【桥】
- 数据库_基础知识_MySQL_UpdateSelect(根据查询出来的结果批量更新)
- web开发的功能备忘录
- JavaScript特效二(tab选项卡、百度输入法下拉列表)
- Bug: LayoutInflater.inflate
- Restful Api的访问控制方法