subject
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node.
Note: leaf nodes refer to nodes without child nodes.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
- Method 1: DFS
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { if(root == NULL) return 0; if(root->left==NULL && root->right==NULL) return 1;//Avoid int at initial_ MAX + 1 causes overflow int min_depth = INT_MAX; if(root->left!=NULL) min_depth = min(minDepth(root->left), min_depth);//Recursive search for left subtree if(root->right!=NULL) min_depth = min(minDepth(root->right), min_depth);//Recursive search right subtree return min_depth+1; } };
-
Time complexity O(n)
-
Spatial complexity O(logn)
-
thinking
- For each non leaf node, its depth is equal to the depth of the smallest leaf node in the left subtree and right subtree + 1, which transforms a problem into a smaller problem
- If the current node is empty, it returns a depth of 0. If its left and right children are empty, it returns a depth of 1 Set min_depth records the current minimum depth.
- If it has left children, the minimum depth at this time is the smaller of the current minimum depth and the minimum depth of its left subtree.
- If it has a right child, the minimum depth at this time is the smaller of the current minimum depth and the minimum depth of its right subtree.
- Returns the minimum depth at this time plus the current node.
-
Method 2: iteration
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { if(root==NULL) return 0;//If the root node is empty, 0 is returned int result = INT_MAX;//Record minimum depth stack<pair<TreeNode*, int>> s;//Auxiliary stack s.push(make_pair(root, 1));//Stack the root node and its depth while(!s.empty()){ //Record the top section of the stack and its depth, and then exit the stack TreeNode *node = s.top().first; int depth = s.top().second; s.pop(); //If it has no left and right children, the minimum depth is the smaller value of the minimum depth at this time and the depth of the node if(node->left==NULL && node->right==NULL) result = min(result,depth); //If its left subtree is not empty and the depth of the node is less than the current minimum depth, the node is stacked if(node->left!=NULL && result>depth) s.push(make_pair(node->left, depth+1)); //If its right subtree is not empty and the depth of the node is less than the current minimum depth, put the node on the stack if(node->right!=NULL && result>depth) s.push(make_pair(node->right, depth+1)); } return result;//Returns the minimum depth } };
- Time complexity O(n)
- Spatial complexity O(n)
- thinking
- Calculate the depth from top to bottom.
- If the root node is not empty, put the root node and its depth 1 on the stack. When the current node has no left and right children, that is, the node is a leaf node, the depth of the current node and the minimum depth are compared.
- If it has a left child and the minimum depth at this time is greater than the depth of the current node, the left child and the depth at this time + 1 are stacked.
- If it has a right child and the minimum depth at this time is greater than the depth of the current node, the right child and the depth at this time + 1 are stacked.
- Judge the next stack top element in the while loop until the stack is empty