int BinaryTree::mindepth(Node* currentNode) { if(currentNode == NULL) return 0; else return 1 + min(maxdepth(currentNode->left),maxdepth(currentNode->right)); }
int BinTree::maxdepth(Node * nd, int level) const { if (nd==nullptr) return level; int l = maxdepth(nd->left, level+1); int r = maxdepth(nd->right, level+1); return l > r ? l : r; }
int maxdepth(TreeNode *node){ if(!node) return 0; int left=maxdepth(node->left); int right=maxdepth(node->right); if(left==-1 || right==-1 || abs(left-right)>1) return -1; else return max(left,right)+1; }
/* * Returns the maximum depth of a tree. */ static int maxdepth(treenode_t *n) { if (n == nullNode) { return 0; } else { return 1 + max(maxdepth(n->left), maxdepth(n->right)); } }
int maxdepth(struct node *root) { int l,r; if(root==NULL) return 0; else { l=maxdepth(root->left); r=maxdepth(root->right); if(l>r) return l+1; else return r+1; } }
int maxdepth( struct node* root ) { int count1 = 1, count2 = 1; if ( root == NULL ) { return 0; } count1 += maxdepth( root->left ); count2 += maxdepth( root->right ); if ( count1 >= count2 ) { return count1; } else { return count2; } }
int main( ) { struct node* root = Build1235( ); int depth = maxdepth( root ); printf( "%d\n", depth ); return 0; }
main() { struct node *root=NULL; int n,choice; printf("Menu \n 1.create\n 2.inorder\n3.preorder \n4.postorder"); while(1) { printf("enter the choice "); scanf("%d",&choice); switch(choice) { case 1: root=create(root); break; case 2: preorder(root); break; case 3: inorder(root); break; case 4: postorder(root); break; case 5: n=sizeoftree(root); printf("the sizeof tree is %d",n); break; case 6: n=maxdepth(root); printf("the max depth is %d",n); break; } } getch(); }
bool BinaryTree::IsBalanced() { return((maxdepth(root) - mindepth(root)) < 2); //return checkBalance(root); -- MyLogic }
bool isBalanced(TreeNode *root) { return maxdepth(root)!=-1; }
int maxdepth(TreeNode<T>* root) { if(root == null) return 0; return 1 + fmax(maxdepth(root->left), maxdepth(root->right)); }
bool isBalancedTree(TreeNode<T>* root) { if ( maxdepth(root)-mindepth(root) <=1 ) return true; return false; }
bool BinTree::isBalanced() const { return maxdepth()-mindepth() > 1 ? false : true; }
int BinTree::maxdepth() const { return maxdepth(root,0); }