高度平衡树

xiaoxiao2022-06-11  33

#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"); }

 

转载请注明原文地址: https://www.6miu.com/read-4930552.html

最新回复(0)