每日算法题【二叉树】:二叉树的最大深度、翻转二叉树、平衡二叉树

(13)二叉树的最大深度
  • [104. 二叉树的最大深度 - 力扣(LeetCode)](https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/)\]:

    递归思路:二叉树的最大深度等于左右子树中深度大的+1

    c 复制代码
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    //二叉树的最大深度等于左右子树中深度大的+1
    int maxDepth(struct TreeNode* root) {
        if (root == NULL) {
            return 0;
        }
        //使用变量保存递归回来的结果进行比较
        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);
    
        return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
    
    }

(14)翻转二叉树
  • [226. 翻转二叉树 - 力扣(LeetCode)](https://leetcode.cn/problems/invert-binary-tree/description/)\]:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* flipTree(struct TreeNode* root) {
    if (root == NULL) {
        return NULL;
    }
    
    // 交换左右子树
    struct TreeNode* temp = root->left;
    root->left = root->right;
    root->right = temp;
    
    // 递归翻转左右子树
    flipTree(root->left);
    flipTree(root->right);
    
    return root;
}

(15)判断一颗树是否是平衡二叉树
  • 110. 平衡二叉树 - 力扣(LeetCode)

  • 解题思路:

    要保证当前树的左右子树高度差不大于1,并且子树本身也是平衡树。

    1. maxDepth函数:递归计算二叉树的高度。
    2. isBalanced函数
      • 如果树为空,返回true
      • 计算左右子树的高度差
      • 如果高度差≤1且左右子树都是平衡的,返回true
      • 否则返回false
    c 复制代码
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    //二叉树的最大深度等于左右子树中深度大的+1
    int maxDepth(struct TreeNode* root) {
        if (root == NULL) {
            return 0;
        }
        //使用变量保存递归回来的结果进行比较
        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);
    
        return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
    }
    
    //通过前序遍历二叉树的最大深度来进行判断
    bool isBalanced(struct TreeNode* root) {
    
        if(root == NULL){
            return true;
        }
    
        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);
    
        //首先要保证当前树的左右子树高度差不大于1,并且子树本身也是平衡树。
        if(abs(leftDepth - rightDepth)<=1 && isBalanced(root->left) && isBalanced(root->right)){
            return true;
        }
    
        return false;
    }
相关推荐
暮冬-  Gentle°1 小时前
C++中的命令模式实战
开发语言·c++·算法
卷福同学3 小时前
【养虾日记】Openclaw操作浏览器自动化发文
人工智能·后端·算法
春日见4 小时前
如何入门端到端自动驾驶?
linux·人工智能·算法·机器学习·自动驾驶
图图的点云库4 小时前
高斯滤波实现算法
c++·算法·最小二乘法
一叶落4385 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
y = xⁿ5 小时前
【LeetCodehot100】2:两数相加 19 删除链表倒数第n个节点
数据结构·链表
老鱼说AI6 小时前
CUDA架构与高性能程序设计:异构数据并行计算
开发语言·c++·人工智能·算法·架构·cuda
罗湖老棍子6 小时前
【例 1】数列操作(信息学奥赛一本通- P1535)
数据结构·算法·树状数组·单点修改 区间查询
big_rabbit05026 小时前
[算法][力扣222]完全二叉树的节点个数
数据结构·算法·leetcode
张李浩7 小时前
Leetcode 15三题之和
算法·leetcode·职场和发展