首页 > 其他 > 详细

Leetcode:面试题55 - II. 平衡二叉树

时间:2020-02-29 09:23:14      阅读:54      评论:0      收藏:0      [点我收藏+]

Leetcode:面试题55 - II. 平衡二叉树

Leetcode:面试题55 - II. 平衡二叉树

Talk is cheap . Show me the 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 isBalance(TreeNode* root,int& height){
        if(root==NULL){
            height=0;
            return true;
        }
        int left,right;
        if(isBalance(root->left,left)&&isBalance(root->right,right)){
            if(abs(left-right)<2){
                height=max(left,right)+1;
                return true;
            } 
        }
        return false;
    }
    bool isBalanced(TreeNode* root) {
        int height=0;
        return isBalance(root,height);
    }
};

Leetcode:面试题55 - II. 平衡二叉树

原文:https://www.cnblogs.com/cell-coder/p/12381509.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!