原题目
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ ``\
9 `20
/``````\
15 ```7
返回它的最大深度 3 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
第二题
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ ```\
15 7
返回它的最小深度 2.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
题目分析
第一题
方法一:递归,从底至上
方法二:dfs,前序遍历
方法三:深搜,层次遍历
方法二和方法三都一样,记录每个节点的层次
第二题
同第一题
完整代码
第一题
方法一
int maxDepth(struct TreeNode* root){
if(root==NULL)return 0;
int rDepth=maxDepth(root->left);
int lDepth=maxDepth(root->right);
return fmax(rDepth,lDepth)+1;
}
方法二:
struct Stack
{
struct TreeNode *node;
int depth;
};
int maxDepth(struct TreeNode* root){
if(root==NULL)return 0;
int max_Depth=1;
struct Stack stack[1000];
int top=0;
stack[top].node=root;
stack[top].depth=1;
top++;
while(top)
{
root=stack[top-1].node;
int cur_Depth=stack[top-1].depth;
top--;
if(root->left)
{
stack[top].node=root->left;
stack[top].depth=cur_Depth+1;
top++;
}
if(root->right)
{
stack[top].node=root->right;
stack[top].depth=cur_Depth+1;
top++;
}
if(max_Depth<cur_Depth)max_Depth=cur_Depth;
}
return max_Depth;
}
方法三:
struct Queue
{
struct TreeNode *node;
int depth;
};
int maxDepth(struct TreeNode* root){
if(root==NULL)return 0;
int max_Depth=1;
struct Queue queue[500];
int front=0,rear=0;
queue[rear].node=root;
queue[rear].depth=1;
rear++;
while(front!=rear)
{
root=queue[front].node;
int cur_Depth=queue[front].depth;
front=(front+1)%500;
if(root->left)
{
queue[rear].node=root->left;
queue[rear].depth=cur_Depth+1;
rear=(rear+1)%500;
}
if(root->right)
{
queue[rear].node=root->right;
queue[rear].depth=cur_Depth+1;
rear=(rear+1)%500;
}
if(max_Depth<cur_Depth)max_Depth=cur_Depth;
}
return max_Depth;
}
第二题
方法一:
int minDepth(struct TreeNode* root){
if(root==NULL)return 0;
if(root->left==NULL&&root->right==NULL)return 1;
int lDepth=INT_MAX,rDepth=INT_MAX;
if(root->left)lDepth=minDepth(root->left);
if(root->right)rDepth=minDepth(root->right);
return fmin(lDepth,rDepth)+1;
}
方法二:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct Stack
{
struct TreeNode *node;
int depth;
};
int minDepth(struct TreeNode* root){
if(root==NULL)return 0;
int max_Depth=INT_MAX;
struct Stack stack[1000];
int top=0;
stack[top].node=root;
stack[top].depth=1;
top++;
while(top)
{
root=stack[top-1].node;
int cur_Depth=stack[top-1].depth;
top--;
if(root->left)
{
stack[top].node=root->left;
stack[top].depth=cur_Depth+1;
top++;
}
if(root->right)
{
stack[top].node=root->right;
stack[top].depth=cur_Depth+1;
top++;
}
if(root->left==NULL&&root->right==NULL&&max_Depth>cur_Depth)
max_Depth=cur_Depth;
}
return max_Depth;
}
方法三:
struct Queue
{
struct TreeNode *node;
int depth;
};
int minDepth(struct TreeNode* root){
if(root==NULL)return 0;
int max_Depth=INT_MAX;
struct Queue queue[500];
int front=0,rear=0;
queue[rear].node=root;
queue[rear].depth=1;
rear++;
while(front!=rear)
{
root=queue[front].node;
int cur_Depth=queue[front].depth;
front=(front+1)%500;
if(root->left)
{
queue[rear].node=root->left;
queue[rear].depth=cur_Depth+1;
rear=(rear+1)%500;
}
if(root->right)
{
queue[rear].node=root->right;
queue[rear].depth=cur_Depth+1;
rear=(rear+1)%500;
}
if(root->left==NULL&&root->right==NULL&&max_Depth>cur_Depth)max_Depth=cur_Depth;
}
return max_Depth;
}