1. 题目

Leetcode#110. 平衡二叉树,C++实现

2. 方法一

2.1. 代码

class Solution {
    bool result=true;
public:
    bool isBalanced(TreeNode* root) {
        Max_depth(root);
        return result;
    }
    int Max_depth(TreeNode* head)
    {
        if(head==NULL) return 0;
        int left=Max_depth(head->left);
        int right=Max_depth(head->right);
        if(abs(left-right)>1)
            result=false;
        return max(left,right)+1;
    }
};

2.2. 结果

Leetcode#110. 平衡二叉树,C++实现

相关文章: