首页 > 代码库 > Leetcode#110 Balanced Binary Tree

Leetcode#110 Balanced Binary Tree

原题地址

 

参考了这篇博文的想法

 

代码:

 1 int balancedp(TreeNode *root) { 2   if (!root) 3     return 0; 4  5   int l = balancedp(root->left); 6   int r = balancedp(root->right); 7   if ((l | r) < 0 || abs(l - r) > 1) 8     return -1; 9   return max(l, r) + 1;10 }11 12 bool isBalanced(TreeNode *root) {13   return balancedp(root) >= 0;14 }

 

Leetcode#110 Balanced Binary Tree