Write a program to find depth of binary search tree without using recursion
Anonym
// Traverse the tree and count the depth at every leaf. The maximum count is // the depth of the tree // SInce recursion is not allowed use a Stack. Any recursive problem can be solved // iteratively by using a stack // Rough skeleton code, without testing class TraverseTree { public: Traverse_Tree { curr_depth = max_depth = 0; } void push(Node& n); int get_depth() { return max_depth } void inc_depth()'; { curr_depth++; } void set_max_depth { if (curr_depth > max_depth) max_depth = curr_depth; } private: int curr_depth; int max_depth; } int bst_depth (const Node& root) { Traverse_Tree t; const Node& n = root; while (n) { t.inc_depth(); t.push(n); if (n->left) { n = n->left continue } else if (n->right) { // same as for left tree ..... } else [ // leaf node t.set_max_depth(); n = t.pop()->right ; t_dec_depth(); } } return t.get_depth();