LeetCode 110. Balanced Binary Tree

Posted StrongYaYa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 110. Balanced Binary Tree相关的知识,希望对你有一定的参考价值。

题目描述:

递归求解

/**
 * 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:
    bool isBalanced(TreeNode* root) {
        if(root == NULL)
            return true;
        if(abs(getdeepth(root->right) - getdeepth(root->left)) > 1)
            return false;
        return isBalanced(root->right) && isBalanced(root->left);
    }
    int getdeepth(TreeNode *node){
        if(node == NULL)
            return 0;
        int l1 = getdeepth(node->left) + 1;
        int l2 = getdeepth(node->right) + 1;
        return max(l1,l2);
    }
};

  

以上是关于LeetCode 110. Balanced Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 110. Balanced Binary Tree 递归求解

[LeetCode]题解(python):110 Balanced Binary Tree

leetcode-110. Balanced Binary Tree

LeetCode_110. Balanced Binary Tree

Leetcode 110. Balanced Binary Tree

leetcode 110. Balanced Binary Tree