Google Meta Amazon Coding Interview Question – Minimum Depth of Binary Tree

Join me to stay up-to-date and get my new articles delivered to your inbox by subscribing here.

March 12, 2022

Amazon Interview  Coding & Algorithm Interview  Google Interview  Meta Interview 

Find the minimum depth (depth of first leaf) of a binary tree.

struct node
 node *left, *right;
 int val;

int min_depth(node* root)
 if (root == NULL) return 0;
 if (root->left == NULL) return min_depth(root->right) + 1;
 if (root->right == NULL) return min_depth(root->left) + 1;
 return 1 + min(min_depth(root->left), min_depth(root->right));