高度平衡树

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };
int dfs(struct TreeNode *root)
{
	int l,r;
	if(root==NULL) return 0;
	l=dfs(root->left);
	if(l<0) return l;
	r=dfs(root->right);
	if(r<0) return r;
	if(abs(l-r)>1) return -1;
	
	return max(l,r)+1;
}
bool isBalanced(TreeNode *root)
{
	return dfs(root) >=0? true:false;
}
int main()
{
	printf("hello world\n");
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/83316536