// Max depth in tree void TreeDictionary::computeDepthHelper(TreeNode * node, int depth, int & currentMaxDepth) { if(node==NULL) { return; } if(depth>currentMaxDepth){ currentMaxDepth++; } if(node->_left!=NULL||node->_right!=NULL) { TreeNode * n = node; computeDepthHelper(n->_right,depth+1,currentMaxDepth); computeDepthHelper(n->_left,depth+1,currentMaxDepth); } }
// Max depth in tree void TreeDictionary::computeDepthHelper(TreeNode * node, int depth, int & currentMaxDepth) { if (node==NULL) return; if ( node->_left == NULL && node->_right == NULL) { // Found a leaf. Update maxDepth and minDepth if ( currentMaxDepth == -1 || depth > currentMaxDepth) { currentMaxDepth = depth; } } computeDepthHelper(node->_left, depth+1, currentMaxDepth); computeDepthHelper(node->_right, depth+1, currentMaxDepth); return; }
int TreeDictionary::maxDepth() { int currentMaxDepth = -1; computeDepthHelper(_root, 0, currentMaxDepth); return currentMaxDepth; }
int TreeDictionary::maxDepth() { int cmd = -1; computeDepthHelper(_root,0,cmd); return cmd; }