LeetCode 111 Minimum Depth of Binary Tree (Minimum Binary Tree Depth) (BT, DFS )(*)

Source: Internet
Author: User

LeetCode 111 Minimum Depth of Binary Tree (Minimum Binary Tree Depth) (BT, DFS )(*)
Translation

Given a binary tree, find its shortest depth. The shortest depth refers to the shortest distance from the node to the nearest leaf node.
Original
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.
Analysis

I thought it was clear that I wrote a function in my previous blog to calculate the height of a binary tree.

LeetCode 110 Balanced Binary Tree (Balanced Binary Tree )(*)

You can use it directly.

The following code calculates the maximum distance between the node and all the leaves. to comply with this question, I have changed the bottom max to min.

int getHeight(TreeNode* root) {    int left = 0, right = 0;    if (!root || (!root->left &&!root->right))        return 0;    if (root->left != NULL)        left = 1 + getHeight(root->left);    if (root->right != NULL)        right = 1 + getHeight(root->right);    return min(left, right);}

Since the distance calculated by the above function does not include the root itself, we need to add 1 when we use it again.

int minDepth(TreeNode* root) {    if (!root) return 0;    return getHeight(root)+1;}

However, I found an error when I submitted it ...... For [1, 2], that is, if the root node is 1 and the left subtree is 2, 2 is expected to be returned, and 1 is returned.

Why? Well, maybe the meaning means that we can only start from the leaves. If one side is empty and the other side is not empty, we need the maximum value.

For example, 3 should also be returned, and only from the left.

     1    /   2  / 3

Modify the Code as follows:

int getHeight(TreeNode* root) {    int left = 0, right = 0;    if (!root || (!root->left && !root->right)) return 0;    if (root->left &&  root->right) {        left = getHeight(root->left) + 1;        right = getHeight(root->right) + 1;        return min(left, right);    }    else {        left = getHeight(root->left) + 1;        right = getHeight(root->right) + 1;        return max(left, right);    }}int minDepth(TreeNode* root) {    if (!root) return 0;    return getHeight(root)+1;}

Well, I found that the left and right variables can be removed:

int getHeight(TreeNode* root) {    if (!root || (!root->left && !root->right)) return 0;    if (root->left &&  root->right) return min(getHeight(root->left) + 1, getHeight(root->right) + 1);    else return max(getHeight(root->left) + 1, getHeight(root->right) + 1);}int minDepth(TreeNode* root) {    if (!root) return 0;    return getHeight(root) + 1;}

In fact, I also found that the + 1 operation is performed on two parameters in the max or min function, so that the + 1 operation can be put out and added only once. Continue to change:

int getHeight(TreeNode* root) {    if (!root || (!root->left && !root->right)) return 0;    if (root->left &&  root->right) return min(getHeight(root->left), getHeight(root->right)) + 1;    else return max(getHeight(root->left), getHeight(root->right)) + 1;}int minDepth(TreeNode* root) {    if (!root) return 0;    return getHeight(root) + 1;}

Then I found that there is no need to write a getHeight function independently. Put it together, and a deep search algorithm will come out ......

int minDepth(TreeNode* root) {    if (!root) return 0;    if (root->left &&  root->right) return min(minDepth(root->left), minDepth(root->right)) + 1;    return max(minDepth(root->left), minDepth(root->right)) + 1;}

Some readers may think that I am so arrogant ...... However, I think this is better. The reason why I love programming is that I like my own algorithms and apps, which can evolve constantly and become more concise and easy to use.

Code
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:    int minDepth(TreeNode* root) {        if (!root) return 0;        if (root->left &&  root->right) return min(minDepth(root->left), minDepth(root->right)) + 1;        return max(minDepth(root->left), minDepth(root->right)) + 1;    }};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.