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

(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;
    }
相关推荐
吃好睡好便好3 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
仰泳之鹅4 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
x_yeyue6 小时前
三角形数
笔记·算法·数论·组合数学
Mr. zhihao7 小时前
深入解析redis基本数据结构
数据结构·数据库·redis
念何架构之路7 小时前
Go语言加密算法
数据结构·算法·哈希算法
AI科技星7 小时前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
失去的青春---夕阳下的奔跑7 小时前
560. 和为 K 的子数组
数据结构·算法·leetcode
黎阳之光8 小时前
黎阳之光:以视频孪生重构智慧医院信息化,打造高标项目核心竞争力
大数据·人工智能·物联网·算法·数字孪生
丷丩8 小时前
三级缓存下MVT地图瓦片服务性能优化策略
算法·缓存·性能优化·gis·geoai-up
m0_629494738 小时前
LeetCode 热题 100-----25.回文链表
数据结构·算法·leetcode·链表