算法力扣刷题记录 四十三【最大、最小深度问题】

前言

本文学习树的深度问题:二叉树(N叉树)最大深度、最小深度;

记录 三十九【层序遍历模版应用二】中解决过二叉树的最大深度和最小深度题目。思路是按层遍历:

  • 最大深度,相当于层序遍历结束;
  • 最小深度,相当于层序遍历过程中判断节点是不是叶子节点。

那么此处的深度,还有什么知识点?


一、最大深度

参考链接给出两种方法:递归法和迭代法。

递归法

以【104.二叉树的最大深度】为题,求递归实现:

递归思路

(1)肯定还是遍历,需要选定一种遍历方式。用后序遍历(左右中)。原因如下:
(2)当前节点的高度 = max(左子树的高度,右子树的高度) +1。累积到整个树的根节点,根节点的高度=整个树的深度。

  • 获取左子树的高度;
  • 再获取右子树的高度;
  • 返回当前节点的高度。
  • 这个顺序是左右中,所以选择后序遍历。这个逻辑也就是一直重复递归的逻辑。

(3)确定递归的返回值:int——高度;递归的参数:TreeNode节点;终止条件:是空节点,返回0。

递归实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int height(TreeNode* cur){
        if(cur == nullptr) return 0;
        int left_height = height(cur->left);
        int right_height = height(cur->right);
        int max = left_height > right_height? left_height:right_height;
        return max+1;
    }
    int maxDepth(TreeNode* root) {
        return height(root);
    }
};

参考学习

  1. 深度:任意一个节点到根节点的距离。
  2. 高度:任意一个节点到叶子节点的距离。
  3. 根节点的高度=树的最大深度。借助高度来求最大深度。
    在这里插入图片描述
  4. 正儿八经求深度,应该是前序遍历(中左右),逐步加1。
  • 用成员变量result代表深入的程度。因为depth在“递”的时候增加,再“归”的时候减少,所以depth不能存放
  • 如果是叶子节点,return;
  • 如果left存在,深入左子树。传递下去深入时,传参depth+1,depth在增加;但是返回后传参depth要减回来(回溯)。
  • 如果right存在,深入右子树。和深入左子树同理。
    (这和递归实现层序遍历同理)
    使用前序遍历实现:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int result;
    void getdepth(TreeNode* cur,int depth){
        result = depth > result ? depth: result;    //最大深度记录到result,因为depth在“递”的时候增加,再“归”的时候减少,所以depth不能存放。
        if(!cur->left && !cur->right) return; //叶子结点返回。depth不增加
        if(cur->left){//左子树有
            getdepth(cur->left,depth+1);//当前深度+1。看进到左子树之后,是否还能再加
        }
        if(cur->right){//和左子树同理
            getdepth(cur->right,depth+1);
        }
        return;
    }
    int maxDepth(TreeNode* root) {
        result = 0;
        if(!root) return result;
        getdepth(root,1);
        return result;
    }
};

迭代法

前言中:记录三十九:层序遍历迭代实现深度求解。


练习题目【559.n叉树的最大深度】

题目

给定一个 N 叉树,找到其最大深度。最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。

示例 1:
在这里插入图片描述

输入:root = [1,null,3,2,4,null,5,6]
输出:3

示例 2:
在这里插入图片描述

输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:5

提示:

树的深度不会超过 1000 。
树的节点数目位于 [0, 10^4] 之间。

思路

N叉树区别在于child个数上,让每个child都进入递归就好。

代码实现

一、递归法,先遍历孩子,再回到自己。采用后序遍历

/*
// 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 getheight(Node* cur){
        if(!cur) return 0;
        int height = 0;
        for(int i = 0;i < cur->children.size();i++){
            int temp = getheight(cur->children[i]);
            height = temp > height ? temp:height;//替换最大值
        }
        return height+1;
    }
    int maxDepth(Node* root) {
        return getheight(root);
    }
};

二、递归法,采用前序遍历,用result记录深度,depth参数在“归”的时候会减少,不能算深度+1

/*
// 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 result;//记录最大深度
    void getdepth(Node* cur,int depth){
        result = depth > result?depth:result;
        if(cur->children.size() == 0) return;//没有孩子。
        for(int i = 0;i < cur->children.size();i++){    //每个孩子遍历。
            getdepth(cur->children[i],depth+1);
        }
        return;
    }
    int maxDepth(Node* root) {
        result = 0;
        if(!root) return result;
        getdepth(root,1);
        return result;
    }
};

三、迭代法,层序遍历:

/*
// 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 maxDepth(Node* root) {
        int depth = 0;
        queue<Node*> que;
        if(!root) return depth;
        que.push(root);
        while(!que.empty()){
            int size = que.size();
            depth++;
            while(size--){
                Node* cur = que.front();que.pop();
                for(int i = 0;i <cur->children.size();i++){
                    que.push(cur->children[i]);
                }
            }
        }
        return depth;
    }
};

二、最小深度

参考链接 肯定还是两种方法:递归和迭代。

  1. 迭代记录 三十九 已经实现。用层序遍历

递归法

题目【111.二叉树的最小深度】,求一个二叉树的最小深度。

思路

如果把求最大深度中获取左右子树高度的最大值,改成获取最小值?可行吗?不行
解释:如果一遍没有子树,也就没有叶子节点,无需考虑。

(1)如果设定初始深度为INT_MAX,遇到一个叶子节点,判断它的深度<当前记录的最小深度,更新记录。所以这种思路是正向求深度,和“最大深度”章节中的前序遍历类似。所以确定使用前序遍历
(2)确定递归返回值:最小深度用一个变量result记录,比较depth<result,所以无需返回值。
(3)确定递归参数:给根节点和depth。
(4)确定终止条件:遇到叶子节点return。但return之前,判断这个叶子节点的深度<当前记录的最小深度result吗?是,更新result记录后return。
(5)逻辑:如果不是叶子结点,遵循中左右顺序遍历。递归之前if(节点不为空)。

代码实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int result;//记录最小深度
    void getdepth(TreeNode* cur,int depth){
        if( cur->left == nullptr && cur->right == nullptr){
            result = depth < result ? depth:result;//遇到叶子节点,更新最小深度。判断可能性。
            return;
        } 
        if(cur->left){
            getdepth(cur->left,depth+1);
        }
        if(cur->right){
            getdepth(cur->right,depth+1);
        }
        return;
    }
    int minDepth(TreeNode* root) {
       result = INT_MAX;
       if(!root) return 0;
       getdepth(root,1);
       return result;
    }
};

参考学习

  1. 上面的思路过程是前序遍历,从上向下求深度。其实后序遍历(左右中),从下向上求高度也可以,由中间节点返回高度。所以这部分学习后序遍历思路。
  2. 那么后序,如果求的是最小高度呢?
  • 确定返回值和参数:返回值int;参数Treenode*。
  • 确定终止条件:cur == nullptr,return 0;
  • 确定逻辑:
    • 先左:cur->left。获取左子树高度;
    • 再右:cur->right。获取右子树高度;
    • 中间:区别——有一边是空子树,选另一边子树高度+1;如果两边都是空,或都不为空,选最小高度+1
  1. 代码实现

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        int getheight(TreeNode* cur){
            if(cur == nullptr){
                return 0;
            }
            int leftheight = getheight(cur->left);
            int rightheight = getheight(cur->right);
            if(leftheight == 0 && rightheight != 0){	//高度为0,代表子树为空
                return rightheight+1;
            }else if(leftheight !=0 && rightheight == 0){
                return leftheight+1;
            }else{
                return min(leftheight,rightheight)+1;
            }
        }
        int minDepth(TreeNode* root) {
            return getheight(root);
        }
    };
    

总结

本文针对二叉树最大深度、最小深度问题作出讨论——思维导图
在这里插入图片描述
(欢迎指正,转载标明出处)

  • 31
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值