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

(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;
    }
相关推荐
A尘埃7 小时前
保险公司车险理赔欺诈检测(随机森林)
算法·随机森林·机器学习
大江东去浪淘尽千古风流人物7 小时前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
独好紫罗兰8 小时前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n8 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
努力学算法的蒟蒻8 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_841495648 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦8 小时前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
独好紫罗兰9 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495649 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
budingxiaomoli9 小时前
优选算法-字符串
算法