代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

打卡第十六天,今天学习二叉树的深度

今日任务

  • 104.二叉树的最大深度
  • 559.n叉树的最大深度
  • 111.二叉树的最小深度
  • 222.完全二叉树的节点个数
  • 二叉树节点的深度:指从节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)

而根节点的高度就是二叉树的最大深度

前序遍历适合用来计算深度:前序遍历是先处理结点,再处理左右孩子,可以从根节点到该结点计算。
后序遍历适合用来计算高度:后续遍历是先处理孩子结点,最后处理本结点,可以从该节点到叶子节点计算

104. 二叉树的最大深度

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。

代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_第1张图片

我的题解

求二叉树的最大深度,根节点到最远的叶子结点的最长路径边数。当层序遍历的时候可以计算二叉树的层数,刚好等于二叉树的最大深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if(root) que.push(root);
        int res = 0;
        while(!que.empty()) {
            int size = que.size();
            res++;
            while(size--) {
                TreeNode *cur = que.front();
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return res;
    }
};

代码随想录

递归法
后序遍历

本题虽然是求深度,但是是求最大深度,其实等于根节点的高度,所以可以用后序遍历完成。

  1. 确认递归的参数和返回值:参数就是传入该数的结点,返回值为深度,所以返回类型为int

  2. 确定递归出口:当结点为空,返回0,表示高度为0,

  3. 单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

class Solution {
public:
    int getdepth(TreeNode * node) {
        if(node == NULL) return 0;
        int ldepth = getdepth(node->left); // 左
        int rdepth = getdepth(node->right); // 右
        int depth = 1 + max(ldepth, rdepth); //中
        return depth;
    }
    int maxDepth(TreeNode* root) {
        return getdepth(root);
    }
};
前序遍历
class solution {
public:
    int result;
    void getdepth(Treenode* node, int depth) {
        result = depth > result ? depth : result; // 中

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

代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_第2张图片
代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_第3张图片

我的题解

class Solution {
public:
    int maxDepth(Node* root) {
        queue<Node*> que;
        if(root) que.push(root);
        int res = 0;
        while(!que.empty()) {
            int size = que.size();
            res++;
            while(size--) {
                Node *cur = que.front();
                que.pop();
                for(int i = 0; i < cur->children.size(); i++) que.push(cur->children[i]);
            }
        }
        return res;
    }
};

代码随想录

class solution {
public:
    int maxdepth(node* root) {
        if (root == 0) return 0;
        int depth = 0;
        for (int i = 0; i < root->children.size(); i++) {
            depth = max (depth, maxdepth(root->children[i]));
        }
        return depth + 1;
    }
};

111.二叉树的最小深度

给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。

代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_第4张图片

我的题解

还是利用层序遍历,当我们发现队头结点的左节点或右节点为空,那我们就找到二叉树的最小深度,根节点到最近的叶子结点距离。

class Solution {
public:
    int minDepth(TreeNode* root) {
       int res = 0;
        queue<TreeNode*> que;
        if(root) que.push(root);
        while(!que.empty()) {
            int size = que.size();
            res++;
            while(size--) {
                TreeNode *cur = que.front();
                que.pop();
                if(cur->left == NULL && cur->right == NULL) return res;
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return res;
    }
};

前序遍历

class Solution {
public:
    int result = INT_MAX;
    void getdepth(TreeNode* node, int depth) {
    	// 中,什么都不做。
        if (node->left == NULL && node->right == NULL) {
            result = depth < result ? depth : result;
            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 minDepth(TreeNode* root) {
        if(root == NULL) return 0;
        getdepth(root, 1);
        return result;
    }
};

代码随想录

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

什么是叶子节点,左右孩子都为空的节点才是叶子节点!

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == NULL) return 0;
        int leftDepth = getDepth(node->left);           // 左
        int rightDepth = getDepth(node->right);         // 右
                                                        // 中
        // 当一个左子树为空,右不为空,这时并不是最低点
        if (node->left == NULL && node->right != NULL) { 
            return 1 + rightDepth;
        }   
        // 当一个右子树为空,左不为空,这时并不是最低点
        if (node->left != NULL && node->right == NULL) { 
            return 1 + leftDepth;
        }
        int result = 1 + min(leftDepth, rightDepth);
        return result;
    }

    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

222.完全二叉树的节点个数

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_第5张图片
代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_第6张图片

我的题解

计算结点数,用前后序遍历,层序遍历,都能很好解决这个问题,当处理结点的时候,总结点数加加,最后返回结果

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == NULL) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};
class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root) que.push(root);
        int nums = 0;
        while(!que.empty()) {
            int size = que.size();
            while(size--) {
                TreeNode *cur = que.front();
                que.pop();
                nums++;
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return nums;
    }
};

代码随想录

完全二叉树做法

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树

如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。

判断其子树是不是满二叉树,如果是则利用公式计算这个子树(满二叉树)的节点数量,如果不是则继续递归

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

你可能感兴趣的:(算法,算法,leetcode,数据结构)