您的位置:首页 > 其它

leetcode-111-Minimum Depth of Binary Tree

2015-07-23 22:04 274 查看


Minimum Depth of Binary Tree



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;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int solve(TreeNode* root,int n) {
if (!root->left && !root->right) return n + 1;// 到了叶子节点 就返回深度
else if (!root->left && root->right) return solve(root->right,n + 1); // 左节点为空  访问右节点
else if (root->left && !root->right) return solve(root->left,n + 1);  // 右节点为空  访问左节点
else {
return min(solve(root->left,n + 1),solve(root->right,n + 1)); // 返回左右节点的最小深度
}
}
int minDepth(TreeNode* root) {
if (!root) return 0;
return solve(root,0);
}
};


另外一种递归
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int solve(TreeNode* root,int n) {
if (!root) return 99999;   // 为空则 返回一个无穷大的值
if (!root->left && !root->right) return n + 1;  //  <span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">到了叶子节点 就返回深度</span>
else {
return min(solve(root->left,n + 1),solve(root->right,n + 1));   <span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">//  非叶子节点  返回左右节点的最小深度</span>
}
}
int minDepth(TreeNode* root) {
if (!root) return 0;
return solve(root,0);
}
};


非递归求解
用队列实现BSF,层次遍历二叉树,找到有叶子节点所在的层数。 参考 点击打开链接
/**
* Definition for a binary tree node.
* 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) return 0;

int ans = 0;  // 层数 即深度
int count_ = 1; // 每一层的节点数
TreeNode* t;
queue<TreeNode* > q;
q.push(root);
int f = 0;
while (!q.empty()) {
t = q.front();
q.pop();
count_--;

if (!t->left&&!t->right) f = 1; // 找到一个深度最小的叶子节点  标记这一层为深度最小的一层
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);

if (count_ == 0) {  // 该层的节点访问完
count_ = q.size(); // 下一层的节点数
ans ++;
if (f) break;
}

}
return ans;
}
};


way 2:
/**
* Definition for a binary tree node.
* 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) return 0;

int ans = 1;  //  下一层的层数
int count_ = 1; // 每一层的节点数
TreeNode* t;
queue<TreeNode* > q;
q.push(root);
while (!q.empty()) {
t = q.front();
q.pop();
count_--;

if (!t->left&&!t->right) break; // 找到一个深度最小的叶子节点  就结束
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);

if (count_ == 0) {  // 该层的节点访问完
count_ = q.size(); // 下一层的节点数
ans ++;
}
}
return ans;
}
};


今天有想了一下 递归的问题
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int solve(TreeNode* root) {
if (!root) return 0;
else if (root->left && !root->right) return 1 + solve(root->left);
else if (!root->left && root->right) return 1 + solve(root->right);
else return 1 + min(solve(root->right) , solve(root->left));
}
int minDepth(TreeNode* root) {
return solve(root);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: