LeetCode 515在每个树行中找最大值 116 117填充每个节点的下一个右侧节点指针(II) 104二叉树的最大深度 111二叉树的最小深度 | 代码随想录25期训练营day15

文章介绍了如何使用C++解决LeetCode中的四个二叉树相关问题:找到每个树行的最大值、填充每个节点的下一个右侧节点指针、计算二叉树的最大深度和最小深度,通过递归和优先级队列等方法实现。
摘要由CSDN通过智能技术生成

LeetCode 515 在每个树行中找最大值 2023.11.8

class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> result;
        if(root == NULL)
            return result;
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty())
        {
            //用优先级队列,能默认排序从大到小
            priority_queue<int> vec;
            int size = que.size();
            while (size--)
            {
                TreeNode *temp = que.front();
                que.pop();
                vec.push(temp->val);
                if(temp->left)
                    que.push(temp->left);
                if(temp->right)
                    que.push(temp->right);
            }
            //将优先级队列top()最大值记录在答案中
            result.push_back(vec.top());
        }
        return result;
    }
};

LeetCode 116 117填充每个节点的下一个右侧节点指针、II 2023.11.8

class Solution {
public:
    Node* connect(Node* root) {
        if(root == NULL)
            return root;
        queue<Node*> que;
        que.push(root);
        while (!que.empty())
        {
            int size = que.size();
            while (size--)
            {
                Node* temp = que.front();
                que.pop();
                if(temp->left)
                    que.push(temp->left);
                if(temp->right)
                    que.push(temp->right);
                //核心:判断size值,如果不为0,则next指向下一个元素,为零则指向空
                if(size)
                    temp->next = que.front();
                else
                    temp->next = NULL;
            }    
        }
        return root;
    }
};

LeetCode 104 二叉树的最大深度 2023.11.8

class Solution {
public:
    //后序递归遍历
    // int getdepth(TreeNode* root)
    // {
    //     if(root == NULL)
    //         return 0;
    //     int leftdepth = getdepth(root->left);
    //     int rightdepth = getdepth(root->right);
    //     return max(leftdepth, rightdepth) + 1;
    // }

    //前序递归
    int result;
    void getdepth(TreeNode* root, int depth)
    {
        result = depth > result ? depth : result;
        if(root == NULL)
            return;
        if(root->left)
            getdepth(root->left, 1+depth);
        if(root->right)
            getdepth(root->right, 1+depth);
    }

    int maxDepth(TreeNode* root) {
        //层序遍历
        // int num = 0;
        // if(root == NULL)
        //     return num;
        // queue<TreeNode*> que;
        // que.push(root);
        // while (!que.empty())
        // {
        //     int size = que.size();
        //     while (size--)
        //     {
        //         TreeNode *temp = que.front();
        //         que.pop();
        //         if(temp->left)
        //             que.push(temp->left);
        //         if(temp->right)
        //             que.push(temp->right);
        //     }
        //     //核心:每遍历完一层,层数加1
        //     num++;
        // }
        // return num;

        //后序递归遍历
        // return getdepth(root);

        //前序递归调用
        result = 0;
        if(root == NULL)
            return result;
        getdepth(root, 1);
        return result;
    }
};

LeetCode 111 二叉树的最小深度 2023.11.8

class Solution {
public:
    //后序遍历
    int getheight(TreeNode* root)
    {
        if(root == NULL)
            return 0;
        if(root->left != NULL && root->right == NULL)
            return 1 + getheight(root->left);
        else if(root->left == NULL && root->right != NULL)
            return 1 + getheight(root->right);
        else
            return 1 + min(getheight(root->left), getheight(root->right));
    }
    int minDepth(TreeNode* root) {
        //层次遍历
        // int num = 0;
        // if(root == NULL)
        //     return num;
        // queue<TreeNode*> que;
        // que.push(root);
        // while (!que.empty())
        // {
        //     int size = que.size();
        //     num++;
        //     while (size--)
        //     {
        //         TreeNode *temp = que.front();
        //         que.pop();
        //         //如果左右根都无则为叶子结点
        //         if(temp->left == NULL && temp->right == NULL)
        //             return num;
        //         if(temp->left)
        //             que.push(temp->left);
        //         if(temp->right)
        //             que.push(temp->right);
        //     }
        // }
        // return num;

        //后序遍历
        return getheight(root);
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值