代码随想录算法训练营第11天|二叉树part01

226.翻转二叉树 

这道题目 一些做过的同学 理解的也不够深入,建议大家先看我的视频讲解,无论做过没做过,都会有很大收获。

/**
 * 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:
    TreeNode* invertTree(TreeNode* root) {
    if(root==nullptr)
    return root;
    swap(root->left,root->right);
    invertTree( root->left); 
    invertTree(root->right) ;
    return root;
    }
};

题目链接/文章讲解/视频讲解:代码随想录

递归法还是挺简单的,先把自身下面的俩交换了,在让下面的左右各自交换。

/**
 * 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:
    TreeNode* invertTree(TreeNode* root) {
    stack<TreeNode*>st;
    if(root!=nullptr)
    st.push(root);
    while(!st.empty())
    {
        TreeNode* node=st.top();
        st.pop();
        swap(node->left,node->right);
        if(node->left!=nullptr)st.push(node->left);
        if(node->right!=nullptr)st.push(node->right);
    }
    return root;
    }
};

前序遍历法也很简单,没什么难度。经典的,定义栈,交换,压入,弹出。

层序遍历法也不难

/**
 * 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:
    TreeNode* invertTree(TreeNode* root) {
    queue<TreeNode*>que;
    if(root!=nullptr)
    que.push(root);
    while(!que.empty())
    {
        TreeNode* node=que.front();
        que.pop();
        swap(node->left,node->right);
        if(node->left!=nullptr)que.push(node->left);
        if(node->right!=nullptr)que.push(node->right);
    }
    return root;
    }
};

感觉逆转二叉树主要是考虑自己对于二叉树遍历的掌握,整体难度不大,递归法最简单,一直递归。先序遍历用stack,层序遍历用queue

101. 对称二叉树 

/**
 * 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:
    bool compare(TreeNode* left,TreeNode* right)
    {
        if(left==nullptr&&right==nullptr)return true;
        else if(left==nullptr&&right!=nullptr)return false;
        else if(left!=nullptr&&right==nullptr)return false;
        else if(left->val!=right->val)return false;
        else return compare(left->left,right->right)&&compare(left->right,right->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr){
            return true;
        }
        return compare(root->left,root->right);

    }
};

先看视频讲解,会更容易一些。

还是比较简单的,递归的思想真的好重要,前面几行if语句,本质上就是比较两个指针的值是不是相等,但是比较的最大问题是如果是空指针的话就无法比较。所以还是得考虑如果是空指针怎么办,需要很多if语句

题目链接/文章讲解/视频讲解:代码随想录

/**
 * 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:
   
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr){
            return true;
        }
       queue<TreeNode*>que;  
       que.push(root->left);
       que.push(root->right);     
       while(!que.empty())
       { 
        TreeNode* left=que.front();
       que.pop();
        TreeNode* right=que.front();
        que.pop();
        if(!left&&!right) continue;
        if((!left||!right)||(left->val!=right->val)) return false;
que.push(left->left);
que.push(right->right);
que.push(right->left);
que.push(left->right);
       }
       return true;
    }
};

队列法也挺好理解

堆栈法本质上和队列法完全一样。无论是队列还是堆栈,本质上都是存储两两一对。

/**
 * 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:
   
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr){
            return true;
        }
       stack<TreeNode*>st;  
       st.push(root->left);
       st.push(root->right);     
       while(!st.empty())
       { 
        TreeNode* left=st.top();
       st.pop();
        TreeNode* right=st.top();
       st.pop();
        if(!left&&!right) continue;
        if((!left||!right)||(left->val!=right->val)) return false;
st.push(left->left);
st.push(right->right);
st.push(right->left);
st.push(left->right);
       }
       return true;
    }
};

104.二叉树的最大深度 (优先掌握递归)

什么是深度,什么是高度,如何求深度,如何求高度,这里有关系到二叉树的遍历方式。

大家 要先看视频讲解,就知道以上我说的内容了,很多录友刷过这道题,但理解的还不够。

题目链接/文章讲解/视频讲解: 代码随想录

最先想到的便是层次遍历。层次遍历,每遍历一层,层数++,这样给遍历完。

/**
 * 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 maxDepth(TreeNode* root) {     
        if(!root) return 0;
        queue<TreeNode*>que;
        que.push(root);
       int depth=0;
        while(!que.empty())
        {
            int size=que.size();
            for(int i=0;i<size;i++)
            {
                TreeNode*node=que.front();
                que.pop();
                if(node->left)
                que.push(node->left);
                if(node->right)
                que.push(node->right);     
            }
             depth ++;
        }
        return depth;
        }
};

如果采用递归法,没想到竟然两遍直接通过了

/**
 * 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 getDepth(TreeNode* root)
{
    if( root==nullptr )
return 0;
    return max(getDepth(root->left),getDepth(root->right))+1;

}
    int maxDepth(TreeNode* root) {         
        return getDepth(root);
        }
};

就是一个个判断子树的深度。如果子树为空就会返回0,一层层的往上传递。

111.二叉树的最小深度 (优先掌握递归)

先看视频讲解,和最大深度 看似差不多,其实 差距还挺大,有坑。

首先想到的就是层次遍历,遍历到俩子节点都为空,就是多少深度,整个代码本质上就是个深度遍历。

/**
 * 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) return 0;
        queue<TreeNode*>que;
        que.push(root);
       int depth=1;
        while(!que.empty())
        {
            int size=que.size();
            for(int i=0;i<size;i++)
            {
                TreeNode*node=que.front();
                que.pop();
               if(!node->left&&!node->right)
               {
                return depth++;
               }
                else{
                    if(node->left)
                    que.push(node->left);
                    if(node->right)
                    que.push(node->right);
                    }
            }
             depth ++;
        }
        return depth;
    
    }
};

然后是递归,也很简单,主要是判断条件,比最大的麻烦多了。因为没有左子树或者右子树并不是最小深度,所以麻烦一些。

/**
 * 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 getDepth(TreeNode* node)
{
    if(!node->left&&!node->right)
    return 1;
    if(!node->left&&node->right)
    return 1+getDepth(node->right);
    if(node->left&&!node->right)
    return 1+getDepth(node->left);
    return min(getDepth(node->left),getDepth(node->right))+1;

}
    int minDepth(TreeNode* root) {
        if (!root)
        return 0;
return getDepth(root);
    }
};

题目链接/文章讲解/视频讲解:代码随想录

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码随想录算法训练营是一个优质的学习和讨论平台,提供了丰富的算法训练内容和讨论交流机会。在训练营中,学员们可以通过观看视频讲解来学习算法知识,并根据讲解内容进行刷题练习。此外,训练营还提供了刷题建议,例如先看视频、了解自己所使用的编程语言、使用日志等方法来提高刷题效果和语言掌握程度。 训练营中的讨论内容非常丰富,涵盖了各种算法知识点和解题方法。例如,在第14训练营中,讲解了二叉树的理论基础、递归遍历、迭代遍历和统一遍历的内容。此外,在讨论中还分享了相关的博客文章和配图,帮助学员更好地理解和掌握二叉树的遍历方法。 训练营还提供了每日的讨论知识点,例如在第15的讨论中,介绍了层序遍历的方法和使用队列来模拟一层一层遍历的效果。在第16的讨论中,重点讨论了如何进行调试(debug)的方法,认为掌握调试技巧可以帮助学员更好地解决问题和写出正确的算法代码。 总之,代码随想录算法训练营是一个提供优质学习和讨论环境的平台,可以帮助学员系统地学习算法知识,并提供了丰富的讨论内容和刷题建议来提高算法编程能力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [代码随想录算法训练营每日精华](https://blog.csdn.net/weixin_38556197/article/details/128462133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值