【LeetCode刷题日志】:二叉树的所有路径、找树左下角的值

114 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第11天,点击查看活动详情

1、写在前面

大家好,这里是【LeetCode刷题日志】。今天的两道题分别是:

  • 二叉树的所有路径
  • 找树左下角的值

2、内容

2.1、题目一

链接:257. 二叉树的所有路径 - 力扣(LeetCode)

(1) 描述

image.png

(2) 举例

image.png

image.png

(3) 解题

参考代码:

递归

/**
 * 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:
    // 自定义递归函数(前序遍历:中 左 右)
    void func(TreeNode* cur, vector<int>& path, vector<string>& result) {
        // 结点处理逻辑
        path.push_back(cur->val);
        // 如果遇到叶子节点,则将当前存储的路径压入结果中,并结束当前递归
        if (cur->left == NULL && cur->right == NULL) {
            // 按照题目要求的路劲格式进行存储
            string str;
            for (int i = 0; i < path.size() - 1; i++) {
                str += to_string(path[i]);
                str += "->";
            }
            str += to_string(path[path.size() - 1]);
            // 将str导入结果集中
            result.push_back(str);
            // 结束当前递归
            return;
        }
        // 如果当前节点的左孩子不为空,则向左访问
        if (cur->left) {
            func(cur->left, path, result);
            path.pop_back();
        }
        // 如果当前节点的右孩子不为空,则向右访问
        if (cur->right) {
            func(cur->right, path, result);
            path.pop_back();
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;  // 结果集
        vector<int> path;       // 存储每条路径
        if (root == NULL) return result;
        func(root, path, result);
        return result;
    }
};

广度优先遍历

class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;              // 定义一个result
        if (root == NULL) return result;    // 如果根节点为空,则返回空数组
        queue<TreeNode*> nodeQueue;        // 存储结点的队列
        queue<string> pathQueue;           // 存储路径的队列
        nodeQueue.push(root);              // 入队根结点
        pathQueue.push(to_string(root->val));  // 入队根结点的值
        // 当结点队列为空时退出循环
        while (!nodeQueue.empty()) {
            // 取到 结点 队列的队头元素
            TreeNode* node = nodeQueue.front();    nodeQueue.pop();
            // 取到 路径 队列的队头元素
            string path = pathQueue.front();       pathQueue.pop();
            
            // 如果遇到叶子结点,则压入完整路径
            if (node->left == NULL && node->right == NULL) {
                result.push_back(path);
            } else {
                // 如果结点的左孩子不为空,则入队左孩子,并且在路径尾部添加上左孩子的值
                if (node->left != NULL) {
                    nodeQueue.push(node->left);
                    pathQueue.push(path + "->" + to_string(node->left->val));
                }
                // 如果结点的右孩子不为空,则入队右孩子,并且在路径尾部添加上右孩子的值
                if (node->right != NULL) {
                    nodeQueue.push(node->right);
                    pathQueue.push(path + "->" + to_string(node->right->val));
                }
            }
        }
        // 最后返回结果
        return result;
    }
};

2.2、题目二

链接:513. 找树左下角的值 - 力扣(LeetCode)

(1) 描述

image.png

(2) 举例

image.png

image.png

(3) 解题

参考代码:

 // 广度优先遍历
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;   // 定义一个工作队列
        que.push(root);         // 将根节点入队
        int result = root->val; // result初始化为根结点的值
        while(!que.empty()) {
            // 取到当前队列的大小
            int size = que.size();
            // 遍历前size个元素
            for(int i = 0; i<size; i++) {
                // 取到队头元素
                TreeNode* node = que.front(); que.pop();
                // 取到是每层的第一个元素
                if(i == 0) result = node->val;
                // 入队当前结点的左右孩子
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        // 最后返回结果
        return result;
    }
};

3、写在最后

好的,今天就先刷到这里。