110. Balanced Binary Tree

problem

110. Balanced Binary Tree

 code

回归方法

/**
 * 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(getDepth(root->left)-getDepth(root->right))>1) return false;
        return isBalanced(root->left) && isBalanced(root->right);
        
    }
    
    int getDepth(TreeNode* root)
    {
        if(root==NULL) return 0;
        return 1+max(getDepth(root->left), getDepth(root->right));
    }
    
};
View Code

 

 

 参考

1. Balanced Binary Tree;

2. BBT;

posted on 2018-11-27 20:04  鹅要长大  阅读(140)  评论(0编辑  收藏  举报

导航