Minimum Depth of Binary Tree [LeetCode]

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Summary: BFS or DFS with recursion.

 1     int minDepth(TreeNode *root) {

 2         if(root == NULL)

 3             return 0;

 4         if(root->left == NULL && root->right == NULL)

 5             return 1;

 6         if(root->right == NULL)

 7             return minDepth(root->left) + 1;

 8         if(root->left == NULL)

 9             return minDepth(root->right) + 1;

10         return min(minDepth(root->left), minDepth(root->right)) + 1;

11     }

 

你可能感兴趣的:(LeetCode)