《录鼎记》第十四章——二叉树+递归算法

今日内容:

  • 104.二叉树的最大深度
  • 559.n叉树的最大深度
  • 111.二叉树的最小深度
  • 222.完全二叉树的节点个数

一、二叉树的最大深度

力扣题目链接 (opens new window)

思路:初步想的还是昨天的层序遍历,,毕竟昨天刚做过,还有印象。

但今天的目标是递归法,所以先说递归。

先说后序遍历——后序遍历求的是根节点的高度。

递归三部曲:由于要求深度,返回值要为int型,参数则为根节点(唯一的已知信息)

int getdepth(treenode* node)

2、终止条件:遇到空节点

if (node == NULL) return 0;

3、单层递归逻辑

int leftdepth = getdepth(node->left);       // 左
int rightdepth = getdepth(node->right);     // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;
/**
 * 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 compare(TreeNode* node){
        if(node==NULL){
            return 0;
        }
        int leftdepth =compare(node->left);
        int rightdepth =compare(node->right);
        int depth = 1+max(leftdepth,rightdepth);
        return depth;
    }
    int maxDepth(TreeNode* root) {
        int depth =compare(root);
        return depth;
        

    }
};

看着代码的样子就很像能简化,但没想到简化后如此简单。

class Solution {
public:
   
    int maxDepth(TreeNode* root) {
        if(root==NULL){
            return 0;
        }
        return 1+max(maxDepth(root->left),maxDepth(root->right));
        

    }
};

前序递归由于他是优先遍历中间节点,所以写这个就有一定的困难。

如果是用层序遍历,这题就是个模板题了

class solution {
public:
    int maxdepth(treenode* root) {
        if (root == NULL) return 0;
        int depth = 0;
        queue<treenode*> que;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++; // 记录深度
            for (int i = 0; i < size; i++) {
                treenode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return depth;
    }
};

二、n叉树的最大深度

力扣题目链接 (opens new window)

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

        
    }
};

迭代法依然用层序遍历即可解决

class solution {
public:
    int maxdepth(node* root) {
        queue<node*> que;
        if (root != NULL) que.push(root);
        int depth = 0;
        while (!que.empty()) {
            int size = que.size();
            depth++; // 记录深度
            for (int i = 0; i < size; i++) {
                node* node = que.front();
                que.pop();
                for (int j = 0; j < node->children.size(); j++) {
                    if (node->children[j]) que.push(node->children[j]);
                }
            }
        }
        return depth;
    }
};

三、二叉树的最小深度

力扣题目链接 (opens new window)

思路:最小深度是从根节点到最近叶子节点最短路径上的节点数量,叶子节点即是左右节点为空才为叶子节点。

递归三部曲:1、确定参数和返回值      需要返回最小深度所以函数类型为int,参数则为Treenode节点。

int getDepth(TreeNode* node)

2、终止节点也就是遇到了空节点,要返回0;

if (node == NULL) return 0;

3、单层递归逻辑

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;
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) {
        if(root==0) return 0;
        int depth = getdepth(root);
        return depth;

    }
};

以上是最终代码,感觉需要看几遍才能理解。

二叉树操作还不熟练,所以精简代码还只是看懂阶段:

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

前序遍历的话最主要是顺序的变化

class Solution {
private:
    int result;
    void getdepth(TreeNode* node, int depth) {
        if (node->left == NULL && node->right == NULL) {
            result = min(depth, result);  
            return;
        }
        // 中 只不过中没有处理的逻辑
        if (node->left) { // 左
            getdepth(node->left, depth + 1);
        }
        if (node->right) { // 右
            getdepth(node->right, depth + 1);
        }
        return ;
    }

public:
    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        result = INT_MAX;
        getdepth(root, 1);
        return result;
    }
};

迭代法:两个子树均不存在则返回。

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

    }
};

四、完全二叉树的节点个数

力扣题目链接 (opens new window)

class Solution {
public:
    
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;
        int depth =0;
        depth=(1+countNodes(root->left)+countNodes(root->right));
        return depth;


    }
};

先写了一遍,但是不是纯正的递归。

递归三步走:

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
int getNodesNum(TreeNode* cur)
  1. 2、确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
if (cur == NULL) return 0;
  1. 3、确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
int leftNum = getNodesNum(cur->left);      // 左
int rightNum = getNodesNum(cur->right);    // 右
int treeNum = leftNum + rightNum + 1;      // 中
return treeNum;
class Solution {
private:
    int getNodesNum(TreeNode* cur) {
        if (cur == NULL) return 0;
        int leftNum = getNodesNum(cur->left);      // 左
        int rightNum = getNodesNum(cur->right);    // 右
        int treeNum = leftNum + rightNum + 1;      // 中
        return treeNum;
    }
public:
    int countNodes(TreeNode* root) {
        return getNodesNum(root);
    }
};
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;
        while(left){
            left=left->left;
            leftDepth++;
        }
        while(right){
            right=right->right;
            rightDepth++;
        }
        if(leftDepth==rightDepth){
            return(2<<leftDepth)-1;
        }
        return countNodes(root->left) + countNodes(root->right) + 1;


    }
};

有点难以理解,但仔细想还是能想出来。

就是如果left和right满子树相同,则返回左子树平方-1

如果不同,则返回左右子树递归加根节点

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值