day32

【0110.平衡二叉树】

/**
 * 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 depth(TreeNode* root) {
        if (root == NULL)   return 0;
        int result = 1;
        if (root->left != NULL || root->right != NULL)  result++;
        if (root->left)     depth(root->left);
        if (root->right)    depth(root->right);
        return result;
    }
    bool isBalanced(TreeNode* root) {
        if (root == NULL)   return true;
        int leftDepth = depth(root->left);
        int rightDepth = depth(root->right);
        if (((leftDepth - rightDepth) * (leftDepth - rightDepth)) >1)
            return false;
        bool result1 = isBalanced(root->left);
        bool result2 = isBalanced(root->right);
        return (result1 && result2); 
    }
};
/**
 * 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 depth(TreeNode* root) {
        if (root == NULL)   return 0;
        int result1 = 1;
        int result2 = 1;
        if (root->left) {
            depth(root->left);
            result1++;
        }
        if (root->right){
            depth(root->right);
            result2++;
        }    
        return max(result1, result2);
    }
    bool isBalanced(TreeNode* root) {
        if (root == NULL)   return true;
        int leftDepth = depth(root->left);
        int rightDepth = depth(root->right);
        if (((leftDepth - rightDepth) * (leftDepth - rightDepth)) >1)
            return false;
        bool result1 = isBalanced(root->left);
        bool result2 = isBalanced(root->right);
        return (result1 && result2); 
    }
};
  • 上面这两段代码 思路是一样的 只是实现方式有几句不一样 。。。不过代码都是不正确的 只能运行成功二百多各用例 有一些是用例是无法成功运行的

  • 看了卡哥代码 。。。。这里头东西不少啊。。。。有些迷。。。看得我都快睡着了 愣是灵魂出窍了好一阵。。。。

  • 首先 关于 深度、高度的概念、计算方式、和代码实现的碎碎念

  • 其次 是本题递归法和迭代法-----前者相对来说更容易实现 的极限拉扯

  • 首先 关于 深度、高度的概念、计算方式、和代码实现的碎碎念

    • 概念

      • 二叉树节点的深度:指从节点到该节点的最长简单路径的条数。
      • 二叉树节点的高度:指从节点到叶子节点的最长简单路径的条数。
    • 计算方式

      • 按照上述定义 那么应该按照边数来算 但leetcode中强调的深度和高度很明显是按照节点来计算的 ----------- 也就是说 根节点的深度不是0而是1
    • 代码实现-----先序还是后序遍历呢

      • 因为求深度可以从上到下去查 所以需要前序遍历(中左右)

      • 求高度只能从下到上去查,所以只能后序遍历(左右中)

        但是为什么104.二叉树的最大深度中求的是深度,但用的却是后序遍历呢。那是因为代码的逻辑其实是求的根节点的高度,而根节点的高度可以看成是这棵树的最大深度,所以才可以使用后序遍历。

        在本题中 如果真正求取二叉树的最大深度,采用前序遍历方式,代码应该写成如下-----------------反正我是暂时还没看懂这段代码-

        class Solution {
        public:
            int result;
            void getDepth(TreeNode* node, int depth) {
                result = depth > result ? depth : result; // 中
        
                if (node->left == NULL && node->right == NULL) return ;
        
                if (node->left) { // 左
                    depth++;    // 深度+1
                    getDepth(node->left, depth);
                    depth--;    // 回溯,深度-1
                }
                if (node->right) { // 右
                    depth++;    // 深度+1
                    getDepth(node->right, depth);
                    depth--;    // 回溯,深度-1
                }
                return ;
            }
            int maxDepth(TreeNode* root) {
                result = 0;
                if (root == NULL) return result;
                getDepth(root, 1);
                return result;
            }
        };
        

        可以看出使用了前序(中左右)的遍历顺序,这才是真正求深度的逻辑!

  • 其次 是本题递归法和迭代法-----前者相对来说更容易实现 的极限拉扯

    • 递归法 思路是 分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了。 关于分别求出左右子树高度这部分 我的确是分---别了 根本根卡哥思路不一样 也没做对

    • 代码贴上来 以后还得再看看

    • class Solution {
      public:
          // 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
          int getHeight(TreeNode* node) {
              if (node == NULL) {
                  return 0;
              }
              int leftHeight = getHeight(node->left);
              if (leftHeight == -1) return -1;
              int rightHeight = getHeight(node->right);
              if (rightHeight == -1) return -1;
              return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
          }
          bool isBalanced(TreeNode* root) {
              return getHeight(root) == -1 ? false : true;
          }
      };
      
  • 迭代法-----更难懂

class Solution {
private:
    int getDepth(TreeNode* cur) {
        stack<TreeNode*> st;
        if (cur != NULL) st.push(cur);
        int depth = 0; // 记录深度
        int result = 0;
        while (!st.empty()) {
            TreeNode* node = st.top();
            if (node != NULL) {
                st.pop();
                st.push(node);                          // 中
                st.push(NULL);
                depth++;
                if (node->right) st.push(node->right);  // 右
                if (node->left) st.push(node->left);    // 左

            } else {
                st.pop();
                node = st.top();
                st.pop();
                depth--;
            }
            result = result > depth ? result : depth;
        }
        return result;
    }

public:
    bool isBalanced(TreeNode* root) {
        stack<TreeNode*> st;
        if (root == NULL) return true;
        st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();                       // 中
            st.pop();
            if (abs(getDepth(node->left) - getDepth(node->right)) > 1) {
                return false;
            }
            if (node->right) st.push(node->right);           // 右(空节点不入栈)
            if (node->left) st.push(node->left);             // 左(空节点不入栈)
        }
        return true;
    }
};
  • 以后还要再看看
posted @ 2022-12-05 20:41  跬步瑶  阅读(22)  评论(0)    收藏  举报