zoukankan      html  css  js  c++  java
  • [Leetcode] Balanced Binary Tree

    Balanced Binary Tree 题解

    题目来源:https://leetcode.com/problems/balanced-binary-tree/description/


    Description

    Given a binary tree, determine if it is height-balanced.

    For this problem, a height-balanced binary tree is defined as:

    a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

    Example

    Example 1:

    Given the following tree [3,9,20,null,null,15,7]:

    
        3
       / 
      9  20
        /  
       15   7
    
    

    Return true.

    Example 2:

    Given the following tree [1,2,2,3,3,null,null,4,4]:

    
           1
          / 
         2   2
        / 
       3   3
      / 
     4   4
    
    

    Return false.

    Solution

    class Solution {
    private:
        int getHeight(TreeNode *node) {
            if (!node)
                return 0;
            int lh, rh;
            lh = getHeight(node -> left);
            if (lh == -1)
                return -1;
            rh = getHeight(node -> right);
            if (rh == -1)
                return -1;
            int hDiff = lh - rh;
            if (hDiff <= 1 && hDiff >= -1)
                return 1 + max(lh, rh);
            else
                return -1;
        }
    public:
        bool isBalanced(TreeNode* root) {
            if (!root)
                return true;
            return getHeight(root) != -1;
        }
    };
    

    解题描述

    这道题题意是判断一棵二叉树是不是平衡二叉树,思路在于自底向上判断节点是不是满足平衡条件,如果满足的话需要向上层递交下层树的高度。原先的想法是将判断下方树是不是满足条件和计算下方树高度分开成2个函数,但是这样明显时间复杂度较高,并且重复访问了下方节点。于是后面思考了以下,可以将两者二合一,如果下方子树不平衡,其返回的树高就是-1。这样基于后续遍历来递归计算树高就可以判断整棵树的平衡性。

    不过这道题也可以采用迭代的方式来做,采用栈来模拟递归即可:

    class Solution {
    public:
        bool isBalanced(TreeNode* root) {
            if (!root)
                return true;
            stack<TreeNode*> nodeStack;
            unordered_map<TreeNode*, int> heightMap; //节点与节点高度map
            nodeStack.push(root);
            while (!nodeStack.empty()) {
                TreeNode *node = nodeStack.top();
                nodeStack.pop();
                if (node -> left == NULL && node -> right == NULL) {
                    // 叶子节点,高度为1
                    heightMap[node] = 1;
                } else if (node -> left && 
                    heightMap.find(node -> left) == heightMap.end()) {
                    // 左子树非空时,且左子树高度未知时压栈进行计算
                    nodeStack.push(node);
                    nodeStack.push(node -> left);
                } else if (node -> right && 
                    heightMap.find(node -> right) == heightMap.end()) {
                    // 右子树同理
                    nodeStack.push(node);
                    nodeStack.push(node -> right);
                } else {
                    // 获取左右子树树高
                    int lh = (node -> left) ? heightMap[node -> left] : 0;
                    int rh = (node -> right) ? heightMap[node -> right] : 0;
                    int hDiff = lh - rh;
                    // 如果子树上已经不平衡,说明整棵树不是平衡二叉树
                    if (hDiff > 1 || hDiff < -1)
                        return false;
                    heightMap[node] = 1 + max(lh, rh);
                }
            }
            return true;
        }
    };
    
  • 相关阅读:
    微信消息类型和事件类型
    lnmp环境搭建脚本
    laravel框架踩过的坑
    vue结构详解
    PHP消息队列实现及应用
    laravel 运行错误
    笔记分享
    cf730e
    cf 730i
    cf 731f
  • 原文地址:https://www.cnblogs.com/yanhewu/p/8400755.html
Copyright © 2011-2022 走看看