LeetCode 110. 平衡二叉树

LeetCode 110. 平衡二叉树

1、题目

题目链接: 110. 平衡二叉树

给定一个二叉树,判断它是否是 平衡二叉树

示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:true

示例 2:

输入:root = [1,2,2,3,3,null,null,4,4]
输出:false

示例 3:

输入:root = []
输出:true

提示:

  • 树中的节点数在范围 [0, 5000] 内
  • -104 <= Node.val <= 104

2、递归法

思路

代码

cpp 复制代码
#include <iostream>

using namespace std;

//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 getHeight(TreeNode* node) {
        // 如果节点为空,返回高度为0
        if (node == nullptr) {
            return 0;
        }
        // 递归计算左子树的高度
        int leftHeight = getHeight(node->left);
        // 如果左子树高度为-1,说明左子树不是平衡二叉树,直接返回-1
        if (leftHeight == -1) {
            return -1;
        }
        // 递归计算右子树的高度
        int rightHeight = getHeight(node->right);
        // 如果右子树高度为-1,说明右子树不是平衡二叉树,直接返回-1
        if (rightHeight == -1) {
            return -1;
        }
        int result = 0;
        // 如果左右子树的高度差大于1,说明不是平衡二叉树,返回-1
        if (abs(leftHeight - rightHeight) > 1) {
            result = -1;
        } else {
            // 否则,返回左右子树中较高的高度加1
            result = 1 + max(leftHeight, rightHeight);
        }
        return result;
    }

    bool isBalanced(TreeNode* root) {
        return getHeight(root) == -1 ? false : true;
    }
};

int main() {
    TreeNode* root = new TreeNode(3);
    root->left = new TreeNode(9);
    root->right = new TreeNode(20);
    root->right->left = new TreeNode(15);
    root->right->right = new TreeNode(7);
    Solution solution;
    cout << solution.isBalanced(root) << endl;
    return 0;
}

复杂度分析

  • 时间复杂度: O(n)
  • 空间复杂度: O(n)

3、迭代法

思路

代码

cpp 复制代码
class Solution {
private:
    int getDepth(TreeNode* cur) {
        stack<TreeNode*> stk;
        if (cur != NULL) stk.push(cur);
        int depth = 0;
        int result = 0;
        while (!stk.empty()) {
            // 获取栈顶元素
            TreeNode* node = stk.top();
            if (node != NULL) {
                // 如果节点不为空,则将其出栈
                stk.pop();
                // 将节点重新入栈,用于标记该节点已被访问过
                stk.push(node);
                // 将空指针入栈,用于标记该节点的子节点已访问完毕
                stk.push(NULL);
                // 深度加1
                depth++;
                // 如果右子节点存在,则将其入栈
                if (node->right) stk.push(node->right);
                // 如果左子节点存在,则将其入栈
                if (node->left) stk.push(node->left);

            } else {
                // 如果节点为空,则将其出栈
                stk.pop();
                // 获取栈顶元素,即该节点的父节点
                node = stk.top();
                // 将父节点出栈
                stk.pop();
                // 深度减1
                depth--;
            }
            // 更新最大深度
            result = result > depth ? result : depth;
        }
        return result;
    }

public:
    bool isBalanced(TreeNode* root) {
        stack<TreeNode*> stk;
        if (root == nullptr) {
            return true;
        }
        stk.push(root);
        while (!stk.empty()) {
            // 取出栈顶节点
            TreeNode* node = stk.top();
            stk.pop();
            // 判断左右子树高度差是否大于1
            if (abs(getDepth(node->left) - getDepth(node->right)) > 1) {
                return false;
            }
            // 如果右子节点不为空,则将其压入栈中
            if (node->right) stk.push(node->right);
            // 如果左子节点不为空,则将其压入栈中
            if (node->left) stk.push(node->left);
        }
        return true;
    }
};

复杂度分析

  • 时间复杂度: O(n^2)
  • 空间复杂度: O(n)
相关推荐
程序员陆通14 分钟前
Spring Boot RESTful API开发教程
spring boot·后端·restful
程序猿阿伟19 分钟前
《C++高效图形用户界面(GUI)开发:探索与实践》
开发语言·c++
阿客不是客33 分钟前
深入计算机语言之C++:C到C++的过度
c++
LN-ZMOI40 分钟前
c++学习笔记1
c++·笔记·学习
数据分析螺丝钉43 分钟前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
no_play_no_games43 分钟前
「3.3」虫洞 Wormholes
数据结构·c++·算法·图论
￴ㅤ￴￴ㅤ9527超级帅44 分钟前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
五味香44 分钟前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理
无理 Java1 小时前
【技术详解】SpringMVC框架全面解析:从入门到精通(SpringMVC)
java·后端·spring·面试·mvc·框架·springmvc
PYSpring1 小时前
数据结构-LRU缓存(C语言实现)
c语言·数据结构·缓存