代码随想录训练营第十六天——二叉树的最大深度,二叉树的最小深度,完全二叉树的节点个数

二叉树的高度和深度

二叉树节点的深度:根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0还是1开始),深度从根节点往下数
二叉树节点的高度:该节点到叶子节点的最长简单路径的条数或者节点数(取决于高度从0还是1开始),高度从叶子节点往上数

leetcode 104. 二叉树的最大深度

题目链接:二叉树的最大深度

递归法——后序遍历:

根节点的高度就是二叉树的最大深度,所以可以通过求解根节点的高度求二叉树的最大深度
求根节点高度的遍历方式采用后序遍历:左——右-——中(处理逻辑)

class Solution {
public:
    int getheight(TreeNode* node)
    {
        if(node==NULL) return 0;  //终止条件
        int left_height = getheight(node->left);   //左
        int right_height = getheight(node->right); //右
        return 1 + max(left_height,right_height);   //中->逻辑处理
    }
    int maxDepth(TreeNode* root) {
        return getheight(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;
    }
};

简化以上代码:

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

迭代法——层序遍历

迭代法的话,使用层序遍历最为合适,因为二叉树最大的深度就是二叉树的层数,和层序遍历的方式最吻合。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth=0;
        if(root==NULL) return 0;
        que.push(root);
        while(!que.empty())
        {
           int size=que.size();
           depth++;   //每一层深度加1
           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;
    }
};

leetcode 111. 二叉树的最小深度

题目链接:二叉树的最小深度

递归法——后序遍历:

  1. 求最小深度的递归法和求最大深度的递归法不一样。当左子树为空时,右子树不为空,这时并不是最低点,同理,当右子树为空,左子树不为空时,此时也不是最低点。
  2. 如果左子树为空,右子树不为空,最小深度是 1 + 右子树的深度,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。如果左右子树都不为空,返回左右子树深度最小值 + 1 。
class Solution {
public:
    int getDepth(TreeNode* root)
    {
        if(root==NULL) return 0;
        int left_depth = getDepth(root->left);
        int right_depth = getDepth(root->right);
        if(root->left==NULL && root->right!=NULL) return 1 + right_depth;
        if(root->left!=NULL && root->right==NULL) return 1 + left_depth;
        int res =1 + min(left_depth,right_depth);
        return res;
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

迭代法——前序遍历:

class Solution {
private:
    int result;
    void getdepth(TreeNode* node, int depth) {
        // 函数递归终止条件
        if (node == NULL) {
            return;
        }
        // 中,处理逻辑:判断是不是叶子结点
        if (node -> left == NULL && node->right == NULL) {
            result = min(result, depth);
        }
        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;
    }
};

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

题目链接:完全二叉树的节点个数
首先按照普通二叉树的逻辑可以使用后序遍历的递归法和层序遍历的迭代法:

递归法——后序遍历:

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);
    }
};

时间复杂度:O(n)
空间复杂度:O(logn),算上递归系统栈占用的空间

迭代法——层序遍历:

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root==NULL) return 0;
        que.push(root);
        int num=0;
        while(!que.empty())
        {
            int size=que.size();
            num+=size;
            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 num;
    }
};

时间复杂度:O(n)
空间复杂度:O(n)

也可以根据完全二叉树的特性解这道题。完全二叉树只有两种情况:
情况一:满二叉树,此时节点数是 2^树深度-1
情况二:最后一层叶子节点没有满,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
如何判断一个左子树或者右子树是不是满二叉树呢?

  1. 在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树
  2. 在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树

所以整个算法的逻辑就是:遍历到某个节点,判断其子树是不是满二叉树,如果是则利用公式计算这个子树的节点数量,如果不是则继续向下递归。

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == NULL) 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<<n) 相当于2^n,所以leftDepth初始为0
        }  //以上均为递归的终止条件
        //以下为单层递归的逻辑
       int leftTreeNum = countNodes(root->left);       // 左
       int rightTreeNum = countNodes(root->right);     // 右
       int result = leftTreeNum + rightTreeNum + 1;    // 中
       return result;    //以上代码可简化为return countNodes(root->left)+countNodes(root->right)+1;
    }
};

时间复杂度:O(logn * logn)
空间复杂度:O(logn)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值