110. Balanced Binary Tree

110. Balanced Binary Tree

My Submissions
Question
Total Accepted: 101551  Total Submissions: 301899  Difficulty: Easy

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.

Subscribe to see which companies asked this question

class Solution {
public:
    int flag=true;
    int dfs(TreeNode *root)
    {
         if(root==NULL) return 0;
         int h1,h2;
         h1=dfs(root->left);
         h2=dfs(root->right);
         if(abs(h1-h2)>1) flag=0;
         return max(h1,h2)+1;
    }
    bool isBalanced(TreeNode *root) {
        if(root==NULL)return true;
        dfs(root);
        return flag;
    }
};

你可能感兴趣的:(110. Balanced Binary Tree)