【Leetcode长征系列】Balanced Binary Tree

原题:

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1.

思路:递归判断左右子树是否为BST。

代码:

/**
 * Definition for binary tree
 * 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 (treeDepth(root->left)==-1||treeDepth(root->right)==-1) return false;

        return abs(treeDepth(root->left)-treeDepth(root->right))<=1;
    }

    int treeDepth(TreeNode *root){
        if (root==NULL) return 0;
        if (treeDepth(root->left)==-1 || treeDepth(root->right)==-1 || abs(treeDepth(root->left)-treeDepth(root->right))>1) return -1;
        return max(treeDepth(root->left),treeDepth(root->right))+1;
    }
};

超大规模数据集的情况下超时- -#。

这个程序的缺点在于,它需要把整颗树都遍历一遍之后才能给出答案,而其实在扫描过程中一旦遇到左右子树不相同的情况时我们就已经可以结束程序了。像这种递归怎么办比较好呢?参考了网上别人的代码后发现他们多用了一个全局变量。每次多检查一次balanced的值,如果为false直接跳出递归!

class Solution {
public:
    bool balanced = true;

    bool isBalanced(TreeNode *root) {

       treeDepth(root);
       return balanced;
    }

    int treeDepth(TreeNode *root){
        if(!balanced) return -1;
        if (root==NULL) return 0;
        if ( abs(treeDepth(root->left)-treeDepth(root->right))>1 )
            balanced = false;
        return max(treeDepth(root->left),treeDepth(root->right))+1;
    }
};

AC

【Leetcode长征系列】Balanced Binary Tree

时间: 2024-11-08 05:47:00

【Leetcode长征系列】Balanced Binary Tree的相关文章

【一天一道LeetCode】#110. Balanced Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of th

Leetcode solution 110: Balanced Binary Tree

Problem Statement Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example 1: Given

LeetCode题解: Balanced Binary Tree

判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtree

Leetcode题目:Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 题目解答:判断一棵给定的二叉树是不是平衡二叉树.平衡二叉树的条

【LeetCode】110. Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 提示: 此题要求判断一个给定的二叉树是否是“平衡”的,这里“平

leetcode || 110、Balanced Binary Tree

problem: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Hide Tags Tree Depth-first

LeetCode题解之Balanced Binary Tree

1.题目描述 2.问题分析 DFS. 3.代码 1 bool isBalanced(TreeNode* root) { 2 if (root == NULL) 3 return true; 4 5 return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right); 6 7 } 8 9 int

【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal

原题: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:和上一题一样,后续我们可以通过最后一个值得到根的值,同样可以通过定位根的值得到左右子树的子集,递归求解即可. 代码: /** * Definition for binary tree * struct Tre

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined