算法60天训练–9-21-day16

104. 二叉树的最大深度

思路

求最大深度, 首先可以想到,前序遍历和后序遍历

对于求深度,和求高度.都可以用后序遍历,因为一个二叉树的最大深度和最大的高度是相等的

那么如何求最大深度呢?

可以使用递归,即递归的求左右两节点的长度, 并将左右两节点的长度取最大值.

image-20230924185157004

代码

C++

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

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private int getdepth(TreeNode node){
        if(node == null) return 0;
        int leftdepth = getdepth(node.left);
        int rightdepth = getdepth(node.right);
        int depth = 1 + Math.max(leftdepth,rightdepth);
        return depth;
    }
    public int maxDepth(TreeNode root) {
        return getdepth(root);
    }
}

111. 二叉树的最小深度

思路

一眼看其实和求104. 二叉树的最大深度类似,但还是有很多细节的区别的

因为当在根结点有空的子节点的时候就会出现错误

image-20230924185607931

Code

C++

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

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    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 + leftDepth;
        }
        if(node.left == null && node.right != null){
            return 1 + rightDepth;
        }
        int res = 1 + Math.min(leftDepth,rightDepth);
        return res;
    }
    public int minDepth(TreeNode root) {
        return getDepth(root);
    }
}

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

思路

比较容易想到的就是后序遍历,做法跟前两道是一样的.

但因为是一个完全二叉树,根据完全二叉树的性质,我们又可以采用另一种方法

:image-20230924194150634

Code

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int getCount(TreeNode node){
          if(node == null){
            return 0;
        }
        
        int leftCount = getCount(node.left);
        int rightCount = getCount(node.right);
        int countNodes = leftCount + rightCount + 1;
        return countNodes;
    }
    public int countNodes(TreeNode root) {
      return getCount(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; // 这里初始为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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落雨既然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值