代码随想录day16

目录

104.二叉树的最大深度

思路:

559.n叉树的最大深度

思路:

111.二叉树的最小深度

思路:

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

思路:


104.二叉树的最大深度

力扣题目链接(opens new window)

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例: 给定二叉树 [3,9,20,null,null,15,7],

返回它的最大深度 3 。

思路:

深度是由上往下进行运算的,常常采用前序遍历,而高度是由下往上进行运算的,常常采用后序遍历,在计算最大深度时也可以当成是最大高度。

方法一:递归

class solution {
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;
    }
    int maxDepth(TreeNode* root) {
        return getdepth(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叉树的最大深度

力扣题目链接(opens new window)

给定一个 n 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

例如,给定一个 3叉树 :

 返回3.

思路:

要注意构建的不是二叉树了,使用的节点不是树节点了,遍历每层节点用:

i < (node->chirden.size())为条件。

迭代:

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

111.二叉树的最小深度

力扣题目链接(opens new window)

给定一个二叉树,找出其最小深度。

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

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

返回它的最小深度 2.

思路:

计算出左子树与右子树的长度,如果根节点没有右子树或左子树时,要返回另一边的长度。重点注意跟节点要计算进去。

class Solution {
public:
    int get(TreeNode* node){
        if(node ==NULL) return 0;
        int leftlen= get(node->left);
        int rightlen= get(node->right);
        if(leftlen ==0)  return rightlen+1;
        else if(rightlen == 0) return leftlen+1;
        int result=min(rightlen,leftlen);
        return result+1;       
    }
    int minDepth(TreeNode* root) {
       return get(root);
    }
};

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

力扣题目链接(opens new window)

给出一个完全二叉树,求出该树的节点个数。

示例 1:

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

示例 2:

  • 输入:root = []
  • 输出:0

提示:

  • 树中节点的数目范围是[0, 5 * 10^4]
  • 0 <= Node.val <= 5 * 10^4
  • 题目数据保证输入的树是 完全二叉树

思路:

多种方法,个人喜欢迭代,一层一层计算节点。

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

方法二:

class Solution {
public:
   int get(TreeNode* node){
       if(node == NULL)   return 0;
        int leftlen=get(node->left);
        int rightlen=get(node->right);
        int sum=leftlen+rightlen+1;
        return sum;
   }
    int countNodes(TreeNode* root) {
        return get(root);
    }
};

总结:在慢慢补卡当中,虽然已经缺了十几天了。今天写的四道题 ,自己写出了三道,题型比较简单,但还是很开心。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值