C++, learn what balanced tree is.


  • -1
    B

    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      /
      class Solution {
      int reBalanced(TreeNode
      root)
      {
      if(root == NULL)
      return 0;
      int l = reBalanced(root->left);
      int r =reBalanced(root->right);
      if(l<0 || r<0 || abs(l-r)>1)
      return -1;
      else
      return max(l,r)+1;
      }
      public:
      bool isBalanced(TreeNode* root) {
      if(root == NULL)
      return true;
      else
      return (reBalanced(root) != -1);
      }
      };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.