Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by answer man:
// 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();