题目来源
题目描述
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) {
}
};
题目解析
层次遍历
int minDepth(TreeNode* root) {
if(root == NULL){
return 0;
}
std::queue<TreeNode *> queue;
queue.push(root);
int level = 0;
while (!queue.empty()){
level++;
int size = queue.size();
for(int i = 0; i < size; i++){
TreeNode *node = queue.front();
queue.pop();
if(node->left == NULL && node->right == NULL){
return level;
}
if(node->left){
queue.push(node->left);
}
if(node->right){
queue.push(node->right);
}
}
}
return level;
}
DP
算法:分治/动态规划/DFS
实现方式:自底向上/递归
int minDepth(TreeNode* root) {
// 基本情况 dp[null] = 0 ,dp[叶子] = 1
if(root == NULL){
return 0;
}
if(root->left == NULL || root->right == NULL){
return 1;
}
if(root->right == NULL){
return minDepth(root->left) + 1;
}
if(root->left == NULL){
return minDepth(root->right) + 1;
}
// 状态转移方程
// / dp[右孩子] + 1 ,左孩子为null
// dp[非叶子] = dp[左孩子] + 1 ,右孩子为null
// \ min(dp[左孩子], dp[右孩子]) + 1 ,左右孩子都不为null
return std::min(minDepth(root->left), minDepth(root->right)) + 1;
}
回溯
class Solution {
public:
int minDepth(TreeNode* root) {
return helper(root, 1);
}
int helper(TreeNode *root, int depth){
if(root == NULL){
return depth - 1;
}
if(root->left == NULL && root->right == NULL){
return depth ;
}
if(root->left == NULL){
return helper(root->right, depth + 1);
}
if(root->right == NULL){
return helper(root->left, depth + 1);
}
return std::min(helper(root->right, depth + 1), helper(root->left, depth + 1));
}
};
深度优先搜索
class Solution {
private:
int ans = INTMAX_MAX;
public:
int minDepth(TreeNode* root) {
helper(root, 0);
return ans;
}
void helper(TreeNode *root, int depth){
if(root == NULL){
ans = depth;
return ;
}
depth++;
if(root->left == NULL && root->right == NULL){
if(depth < ans){
ans = depth;
}
return;
}
if(root->left == NULL){
helper(root->left, depth);
}
if (root->right != NULL){
helper(root->right, depth);
}
}
};
morris遍历
class Solution {
public:
int minDepth(TreeNode* head) {
if(head == nullptr){
return 0;
}
TreeNode *curr = head;
TreeNode *mostRight = nullptr;
int currLevel = 0;
int minHeight = INT32_MAX;
while (curr != nullptr){
mostRight = curr->left;
if(mostRight != nullptr){
int rightBoardSize = 1;
while (mostRight->right != nullptr && mostRight->right != curr){
rightBoardSize++;
mostRight = mostRight->right;
}
if(mostRight->right == nullptr){ // 第一次到达
currLevel++; // 当前节点一定是从父节点下来的
mostRight->right = curr;
curr = curr->left;
continue;
}else{ // 第二次到达
if (mostRight->left == nullptr) { //此时为叶子节点,因此更新最小深度
minHeight = std::min(minHeight, currLevel);
}
currLevel -= rightBoardSize; //第二次返回父节点(当前节点一定是从子节点上来的),需要更新当前节点
mostRight->right = nullptr;
}
}else{
currLevel++;
}
curr = curr->right;
}
int finalRight = 1;
curr = head;
while (curr->right != nullptr){
finalRight++;
curr = curr->right;
}
if(curr->left != nullptr && curr->right != nullptr){
minHeight = std::min(minHeight, currLevel);
}
return minHeight;
}
};