Pergunta de entrevista da empresa Amazon

Find depth of a binary tree

Respostas da entrevista

Sigiloso

2 de nov. de 2011

int depth(node * root){ if(!root){ return 0; } else { return 1 + max(depth(node->right), depth(node->left)); } }

1

Sigiloso

5 de fev. de 2012

@ajs: don't forget the case of the root being the only node in the tree. If the tree consists of only the root node, then the depth should return 0. There needs to be an else if in there checking for this case, or else your function will return a depth of 1, when it should return 0: if (root.left == null && root.right == null) return 0;

Sigiloso

1 de mar. de 2011

using recursion