111. Minimum Depth of Binary Tree
来源:互联网 发布:h3c 查看端口状态 编辑:程序博客网 时间:2024/05/16 05:20
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 minDepth(TreeNode *root) { if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 1; if (root->left == NULL) return minDepth(root->right) + 1; else if (root->right == NULL) return minDepth(root->left) + 1; else return 1 + min(minDepth(root->left), minDepth(root->right)); }};
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
- 上拉加载下拉刷新(XListViewFooter)
- Android数据库存储sqlite外部存储数据读写
- 66. Plus One
- 上拉加载下拉刷新xlistview
- 上拉加载下拉刷新(XListViewHeader)头部
- 111. Minimum Depth of Binary Tree
- Blender 基础 骨架 01
- mysql小知识点摘抄
- 退出exit,错误处理
- xlistview_footer
- xlistview_header
- HDU-5832 HDU-1212/2016网络选拔赛/大数取余
- Jquery操作下拉框(DropDownList)实现取值赋值
- SAP MM 库存初始化和批量扩充物料仓位