leetCode(20):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.

方法一:求出左右子树的深度,如果差值大于1,则返回false,否则递归判断其左右孩子结点。但该方法每一次递归都要计算一次子结点的深度,出现重复计算。

int maxDepth(TreeNode* root)
{
	if(NULL==root)
		return 0;
	int left=maxDepth(root->left);
	int right=maxDepth(root->right);

	return 1+(left>right?left:right);
}

bool isBalanced(TreeNode* root)
{
	if(NULL==root)
		return true;
	int leftLength=maxDepth(root->left);
	int rightLength=maxDepth(root->right);
	int diff=leftLength-rightLength;
	if(diff>1 || diff<-1)
		return false;
	return isBalanced(root->left) && isBalanced(root->right);
}

方法二:设置一个变量,用于记录深度,不必须重复计算。

/**
 * 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,int *depth)
    {
    	if(NULL==root)
    	{
    		*depth=0;
    		return true;
    	}
    	int leftLength,rightLength;
    	if(isBalanced(root->left,&leftLength) && isBalanced(root->right,&rightLength))
    	{//若两棵子树都是平衡二叉树
    		int diff=leftLength-rightLength;
    		if(diff<=1 && diff>=-1)
    		{
    			*depth=1+(leftLength>rightLength?leftLength:rightLength);
    			return true;
    		}
    	}
    	return false;
    }

    bool isBalanced(TreeNode* root) {
        int depth;
    	return isBalanced(root,&depth);
    }
};
时间: 2024-10-13 10:56:10

leetCode(20):Balanced binary tree的相关文章

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

leetcode -day18 Balanced Binary Tree

测试源码: //测试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace std; class base { public: base() { cout<<"base created!"<<endl; } ~base() { cout<<"base destroyed!"<<endl; } };

[LeetCode][JavaScript]Balanced Binary Tree

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. https://leetco

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

[LeetCode][Java] 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 平衡二叉树

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题目:Balanced Binary Tree

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. tags:深度优先遍历 c

【leetcode】Balanced Binary Tree(middle)

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 – Refresh – Balanced Binary Tree

Need a helper function to return the binary tree maximum height. If the return value is -1, it means there is a imbalanced subbranch . 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7