LeetCode110. Balanced Binary Tree [Simple]

Title description:

Given a binary tree, determine whether it is a highly balanced binary tree.

In this question, a highly balanced binary tree is defined as:

The absolute value of the height difference between the left and right subtrees of each node  of a binary tree does not exceed 1.

My solution:

/**
 * 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 {
public:
    int func(TreeNode* root,bool& judge){
        if(root){
            int a=func(root->left,judge);
            int b=func(root->right,judge);
            if(abs(a-b)>1)  judge=false;
            return a>b?a+1:b+1;
        }
        return 0;
    }

    bool isBalanced(TreeNode* root) {
        bool judge=true;
        func(root,judge);
        return judge;
    }
};

66 original articles published · Like1 · Visits 500

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105158914