leetcode——第110题——平衡二叉树

题目:
给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

/**
 * 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 getDepth(TreeNode* cur)
//     {
//         // 递归的终止条件
//         if(cur == nullptr)  return 0;
//         // 递归得到左右子树的高度值
//         int leftDepth = getDepth(cur->left);
//         int rightDepth = getDepth(cur->right);
//         // 对左右子树的高度值进行判断
//         if(leftDepth == -1)
//             return -1;         
//         if(rightDepth == -1) 
//             return -1;  
//         // 这里做差的时候记得 用绝对值 函数
//         if(abs(leftDepth-rightDepth)>1)    
//             return -1;
//         else    
//             return max(leftDepth,rightDepth)+1 ;
//         // // 简化的写法
//     // return abs(leftDepth-rightDepth)>1 ? -1 : max(leftDepth,rightDepth)+1
//     }
//     bool isBalanced(TreeNode* root) {
//         if(getDepth(root)==-1)
//         {
//             return false;
//         }
//         else
//         {
//             return true;
//         }
//     }

/************************前序遍历 的 迭代写法*******************************/
/*
整体思路是:
1)先定义一个函数,专门用来求高度,

2)这个函数通过栈模拟**后序遍历**,来找每一个节点的高度,
其实就是通过求传入节点对应子树的最大深度,将这个最大深度作为其高度。

3)然后再用栈来模拟**前序遍历**,遍历每一个节点的时候,再去再去判断左右孩子的高度是否符合。
*/
    int getDepth(TreeNode* cur)
    {
        stack<TreeNode*> st;
        int result=0;   // 这里的处理方法没有想到
        int depth=0;    // 通过两个变量来对深度进行控制
        if(cur == nullptr)  return result;
        st.push(cur);
        while(!st.empty())
        {
            TreeNode* node = st.top();
            // 如果 node 为非空,则有四个入栈操作。分别是 左右中空,
            // 不同操作次序不同
            if(node != nullptr)
            {
                // 模拟后序遍历入栈  中(紧跟一个空指针标记)-左-右
                //st.pop();  // 没必要出栈再入栈了,因为是同一个节点
                st.push(nullptr);

                // 这里的处理没想到
                depth++;
                // 每走到一个中节点处就做一次 ++ 运算

                if(node->right != nullptr)   st.push(node->right);
                if(node->left != nullptr)   st.push(node->left);
            }
            else
            {
                st.pop();
                node = st.top();
                st.pop();
                // 这里也是没有想到
                depth--;
            }
            // 还有这里也没有想到
            result = result > depth ? result : depth;
            // 通过这样的方式控制 depth 使其可以控制最后获得每个节点的最大深度
        }
        return result;
    }

    bool isBalanced(TreeNode* root)
    {
        stack<TreeNode*> st;
        if(root == nullptr) return true;
        st.push(root);
        while(!st.empty())
        {
            TreeNode* node = st.top();
            if(node != nullptr)
            {
                // 模拟前序遍历入栈 右-左-中(紧跟一个空指针标记)
                st.pop();
                if(node->right != nullptr)   st.push(node->right);
                if(node->left != nullptr)   st.push(node->left);
                st.push(node);
                st.push(nullptr);
            }
            else
            {
                st.pop();
                node = st.top();
                st.pop();
                int leftDepth = getDepth(node->left);
                int rightDepth = getDepth(node->right);
                if(abs(leftDepth-rightDepth)>1)
                    return false;
            }
        }
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值