Leetcode刷题|day16 二叉树

104. 二叉树的最大深度 - 力扣(LeetCode)

首先要清楚二叉树深度和高度的概念,那么二叉树深度与高度的概念是什么呢?

深度:二叉树任意一个节点到根节点的距离;

高度:二叉树任意一个节点到叶子节点的距离。

递归法(前序和后序遍历都可以)

使用前序就是求深度,使用后序求的是高度。

三部曲:

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回值为树的深度;

int getDepth(TreeNode * node)
  1. 确定终止条件:如果为空节点的话,就返回0,表示高度为0;

if(node ==NULL)return 0;
  1. 确定单层递归逻辑:先求左子树的深度,再求右子树的深度,最后取左右深度最大值+1(+1算上当前中间节点)就是目前节点为根节点的树的深度。

int leftdepth =getdepth(node->left);// 左
int rightdepth =getdepth(node->right);// 右
int depth =1+max(leftdepth, rightdepth);// 中
return depth;

整体解答:

classsolution{
public:
    int getdepth(treenode* node){
        if(node ==NULL)return 0;
        int leftdepth =getdepth(node->left);// 左
        int rightdepth =getdepth(node->right);// 右
        int depth =1+max(leftdepth, rightdepth);// 中
        return depth;}
    intmaxdepth(treenode* root){
        returngetdepth(root);}
};

迭代法

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

559. N 叉树的最大深度 - 力扣(LeetCode)

此题与求二叉树的最大深度比较相似,但是需要注意的是,不需要考虑以何种顺序遍历树,直接遍历孩子节点即可!

递归法

class solution{
public:
    intmaxdepth(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;
        }
};

层序法

class Solution {
public:
    int maxDepth(Node* root) {
       if(root==NULL) return 0;
        queue<Node*> que;
        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;
    }
};

111. 二叉树的最小深度 - 力扣(LeetCode)

最小深度:从根节点到最近叶子结点(左右孩子都为空)的最短路径上的节点数量

求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑

后序遍历:

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

前序遍历方式:

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

222. 完全二叉树的节点个数 - 力扣(LeetCode)

普通二叉树解法:

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点。返回就返回以该节点为根节点二叉树的节点数量。

int getNodesNum(TreeNode* cur) {
  1. 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。

if (cur == NULL) return 0;
  1. 确定单层递归逻辑:先求它的左子树的数量,再求右子树的节点数量,最后取总和再加一(加1是因为算上当前中间节点)就是目前节点为根节点的数量。

int leftNum = getNodesNum(cur->left);      // 左
int rightNum = getNodesNum(cur->right);    // 右
int treeNum = leftNum + rightNum + 1;      // 中
return treeNum;
class Solution {
public:
    int getNum(TreeNode* node){
        if(node==NULL) return 0;
        int leftNum = getNum(node->left);
        int rightNum = getNum(node->right);
        int result = leftNum+rightNum+1;
        return result;
    }
    int countNodes(TreeNode* root) {
        return getNum(root);
    }
};

利用完全二叉树的特性解题:

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

满二叉树节点数量:2^深度-1

对于情况一:可以直接用求满二叉树节点公式计算节点数;

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

如何判断一个左子树或者右子树是不是满二叉树?

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

class Solution {
public:
    int getNum(TreeNode* node){
        if(node==NULL) return 0;
        TreeNode* left = node->left;
        TreeNode* right = node->right;
        int leftheight,rightheight = 0;
        while(left){
            left = left->left;
            leftheight++;
        }
        while(right){
            right=right->right;
            rightheight++;
        }
        if(leftheight==rightheight) return(2<<leftheight)-1;
        int leftnum = getNum(node->left);
        int rightnum = getNum(node->right);
        int result = leftnum + rightnum +1;
        return result;
    }
    int countNodes(TreeNode* root) {
        return getNum(root);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值