MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / balancedhelper

Function balancedhelper

CPP/binarytreeuse.cpp:264–294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

262}
263
264pair<bool,int> balancedhelper(binarytree<int>*root)
265{
266 if(root==NULL)
267 {
268 pair<bool,int> p;
269 p.first=true;
270 p.second=0;
271 return p;
272 }
273
274 pair<bool,int> leftans=balancedhelper(root->left);
275 pair<bool,int> rightans=balancedhelper(root->right);
276
277 int leftheight=leftans.second;
278 int rightheight=rightans.second;
279
280 if(abs(leftheight-rightheight)>1)
281 {
282 pair<bool,int> p;
283 p.first=false;
284 p.second=1+max(leftheight,rightheight);
285 return p;
286 }
287
288 pair<bool,int>ans;
289 ans.first=leftans.first && rightans.first;
290 ans.second=1+max(leftans.second,rightans.second);
291
292 return ans;
293
294}
295
296bool istreebalanced(binarytree<int>*root)
297{

Callers 1

istreebalancedFunction · 0.85

Calls 1

maxFunction · 0.50

Tested by

no test coverage detected