0
点赞
收藏
分享

微信扫一扫

LeetCode 二叉树、N叉树的最大深度与最小深度(递归解)


目录

  • ​​104. 二叉树的最大深度​​
  • ​​559. N叉树的最大深度​​
  • ​​111. 二叉树的最小深度​​


之前的笔记中,已经用层序遍历解决过这个问题了


现在试着用深度的解法去求解

104. 二叉树的最大深度

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
**选择深度遍历方式:**由于我们需要返回深度信息,所以选择后序遍历,通过递归函数的返回值做计算树的高度。
1、确定递归函数的参数以及返回值类型
输入树的根结点,返回这棵树的深度

int getDepth(TreeNode* node)

2、确定终止条件:
如果为空结点,返回高度0

if(node == NULL) return 0;

3、确定单层逻辑:
先求左子树深度,再求右子树的深度,最后取左右深度最大的数值+1(算上当前的中间结点),就是当前结点为根节点的树的最大深度。

int left=getDepth(node->left);
int right=getDepth(node->right);
return max(left,right)+1

完整代码

/**
* 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 getDepth(TreeNode* node)
{
if(node == NULL) return 0;
int left=getDepth(node->left);
int right=getDepth(node->right);
return max(left,right)+1;
}
int maxDepth(TreeNode* root) {
int result=getDepth(root);
return result;
}
};

上面的方法使用的是后序遍历求根结点的高度来求二叉树的最大深度。
当然也可以使用前序遍历,更能体现出回溯的思想:

/**
* 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 result;
void getDepth(TreeNode* node,int depth)
{
//中
result = depth > result ? depth:result; //取结果与depth中的较大值。

if(node->left ==NULL && node->right ==NULL) return ;
//左
if(node->left)
{
depth++; //深度+1
getDepth(node->left,depth);
depth--; //回溯,深度-1
}
//右
if(node->right)
{
depth++; //深度+1
getDepth(node->right,depth);
depth--; //回溯,深度-1
}
return ;
}
int maxDepth(TreeNode* root) {
result = 0;
if(root == NULL) return result;
getDepth(root,1);
return result;
}
};

559. N叉树的最大深度

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

例如,给定一个 3叉树 :
LeetCode 二叉树、N叉树的最大深度与最小深度(递归解)_算法

/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;

Node() {}

Node(int _val) {
val = _val;
}

Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/

class Solution {
public:
int getDepth(Node* node)
{
if(node == NULL) return 0;
int maxnum=0;
for(int i=0;i<node->children.size();i++)
{
maxnum=max(maxnum,getDepth(node->children[i]));
}
return maxnum+1;
}
int maxDepth(Node* root) {
int result=getDepth(root);
return result;
}
};

111. 二叉树的最小深度

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

3

/
9 20
/
15 7
返回它的最小深度 2.
**选择深度遍历方式:**由于我们需要返回深度信息,所以选择后序遍历,通过递归函数的返回值做计算树的高度。
1、确定递归函数的参数以及返回值类型
输入树的根结点,返回这棵树的深度

int getDepth(TreeNode* node)

2、确定终止条件:
如果该结点的左右孩子都为空,返回 0

if(node==NULL) return 0;

3、确定单层逻辑:
如果左子树存在,右子树不存在,结果为左子树最小深度+1
如果右子树存在,左子树不存在,结果为右子树最小深度+1
如果两个子树都存在,选择最小的结果+1
如果两个孩子都不存在的话,进入下一个递归地时候会返回0,两个结果都是1,所以这里不需要考虑那样的情况

int leftDepth=getDepth(node->left);
int rightDepth=getDepth(node->right);
if(node->left && !node->right) return leftDepth+1;
if(!node->left && node->right) return rightDepth+1;
int result=1+min(rightDepth,leftDepth);
return result;

完整代码

class Solution {
public:
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
int leftDepth=minDepth(root->left);
int rightDepth=minDepth(root->right);
if(root->left && !root->right) return leftDepth+1;
if(!root->left && root->right) return rightDepth+1;
return 1+min(rightDepth,leftDepth);
}
};


举报

相关推荐

0 条评论