力扣数据结构14天学习计划—day11

力扣102

力扣102—二叉树的层序遍历

题意

解题思路—利用队列实现层序遍历

这题老面孔了 ,直接上代码吧。

class Solution 
{
public:
    vector<vector<int>> levelOrder(TreeNode* root) 
    {
        if(nullptr==root)
            return {};
        queue<TreeNode*> que;
        que.push(root);
        vector<vector<int>>res;
        while(!que.empty())
        {
            int num=que.size(); //当前层有多少个节点
            vector<int>tmp; //存放当前层的遍历结果
            for(int n=0;n<num;n++)
            {
                TreeNode* node=que.front();
                que.pop();
                tmp.emplace_back(node->val);
                if(node->left)
                    que.push(node->left);
                if(node->right)
                    que.push(node->right);
            }
            res.emplace_back(tmp);
        }
        return res;
    }
};

力扣104—二叉树的最大深度

力扣104

题意

 

法1—深度优先搜索(递归)

class Solution 
{
public:
    int get_depth(TreeNode* root)
    {
        if(nullptr==root)
            return 0;
        int left_depth=get_depth(root->left);
        int right_depth=get_depth(root->right);
        return 1+max(left_depth,right_depth);
    }
    int maxDepth(TreeNode* root) 
    {
        if(nullptr==root)
            return 0;
        int left_depth=get_depth(root->left);
        int right_depth=get_depth(root->right);
        return 1+max(left_depth,right_depth);  
    }
};

 法2—广度优先搜索(利用队列,非递归)

class Solution 
{
public:
    int get_depth(TreeNode* root)
    {
        if(nullptr==root)
            return 0;
        int left_depth=get_depth(root->left);
        int right_depth=get_depth(root->right);
        return 1+max(left_depth,right_depth);
    }
    int maxDepth(TreeNode* root) 
    {
        if(nullptr==root)
            return 0;
        
        /*
        法1:深度优先搜索
        int left_depth=get_depth(root->left);
        int right_depth=get_depth(root->right);
        return 1+max(left_depth,right_depth);  
        */

        //法2,广度优先搜索
        queue<TreeNode*>que;
        int res=0;
        que.push(root);
        while(!que.empty())
        {
            int num=que.size();
            while(num--)
            {
                TreeNode* node=que.front();
                que.pop();
                if(node->left)
                    que.push(node->left);
                if(node->right)
                    que.push(node->right);
            }
            res++;
        }
        return res;
    }
};

力扣101—对称二叉树

力扣101

题意

 

 

解法1—递归

class Solution 
{
public:
    bool compare(TreeNode* left,TreeNode* right)
    {
        if(nullptr==left&&nullptr!=right)
            return false;
        else if(nullptr!=left&&nullptr==right)
            return false;
        else if(nullptr==left&&nullptr==right)
            return true;
        else if(left->val!=right->val)
            return false;
        bool outside=compare(left->left,right->right);
        bool in=compare(left->right,right->left);
        return outside&in;
        
    }
    bool isSymmetric(TreeNode* root) 
    {
        if(root==NULL)
            return true;
        return compare(root->left,root->right);
    }
};

 法2—迭代(利用队列)

class Solution 
{
public:
    bool compare(TreeNode* left,TreeNode* right)
    {
        if(nullptr==left&&nullptr!=right)
            return false;
        else if(nullptr!=left&&nullptr==right)
            return false;
        else if(nullptr==left&&nullptr==right)
            return true;
        else if(left->val!=right->val)
            return false;
        bool outside=compare(left->left,right->right);
        bool in=compare(left->right,right->left);
        return outside&in;
        
    }
    bool isSymmetric(TreeNode* root) 
    {
        if(root==NULL)
            return true;
        // return compare(root->left,root->right);
        queue<TreeNode*>que;
        que.push(root->left);
        que.push(root->right);
        while(!que.empty())
        {
            TreeNode* left_node=que.front();
            que.pop();

            TreeNode* right_node=que.front();
            que.pop();

            if(left_node==nullptr&&right_node!=nullptr)
                return false;
            else if(left_node!=nullptr&&right_node==nullptr)
                return false;
            else if(nullptr==left_node&&nullptr==right_node)
                continue;
            else if(left_node->val!=right_node->val)
                return false;
            
            que.push(left_node->left);
            que.push(right_node->right);

            que.push(left_node->right);
            que.push(right_node->left);
        }
        return true;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所向便是光v

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值