408算法题leetcode--第16天

144. 二叉树的前序遍历

cpp 复制代码
/**
 * 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:
    vector<int>ret;

    void pre(TreeNode* root){
        if(root != nullptr){
            ret.push_back(root->val);
            pre(root->left);
            pre(root->right);
        }
    }
    vector<int> preorderTraversal(TreeNode* root) {
        pre(root);
        return ret;
    }
};

102. 二叉树的层序遍历

cpp 复制代码
/**
 * 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:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>ret;
        if(!root) return ret;

        queue<TreeNode*>q;
        q.push(root);
        while(!q.empty()){
            vector<int>level;
            int size = q.size();
            for(int i = 0; i < size; i++){
                TreeNode* temp = q.front();
                q.pop();
                level.push_back(temp->val);
                if(temp->left) q.push(temp->left);
                if(temp->right) q.push(temp->right);
            }
            ret.push_back(level);
        }
        return ret;
    }
};

199. 二叉树的右视图

cpp 复制代码
/**
 * 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:
    vector<int> rightSideView(TreeNode* root) {
        vector<int>ret;
        if(!root) return ret;
        queue<TreeNode*>q;
        q.push(root);
        while(!q.empty()){
            int size = q.size();
            for(int i = 0; i < size; i++){
                TreeNode* temp = q.front();
                q.pop();
                if(i == size - 1){
                    ret.push_back(temp->val);
                }
                if(temp->left) q.push(temp->left);
                if(temp->right) q.push(temp->right);
            }
        }
        return ret;
    }
};

404. 左叶子之和

cpp 复制代码
/**
 * 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 traverse(TreeNode* root, bool is_left){
        if(!root) return 0;  // 节点为空
        if(!root->left && !root->right && is_left) return root->val;  // 叶子节点且为左子树 
        return traverse(root->left, true) + traverse(root->right, false);
    }
    int sumOfLeftLeaves(TreeNode* root) {
        // 三种情况:1. 节点为空;2. 叶子节点且为左子树 ; 3. else
        return traverse(root, false);
    }
};

104. 二叉树的最大深度

cpp 复制代码
/**
 * 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) {
        // 1. 有左右儿子,2. 空, 3. else
        if(!root) return 0;
        int max_v = 1;
        if(root->left) max_v = maxDepth(root->left) + 1;
        if(root->right) max_v = max(max_v, maxDepth(root->right) + 1);
        return max_v;
    }
};
相关推荐
uhakadotcom8 分钟前
快速理解 tiktoken:OpenAI 模型的高效 BPE 分词器
算法·面试·github
鲸鱼宝宝成长记12 分钟前
[蓝桥杯青少年组省赛 2024] 通关游戏的最少能量值
数据结构·算法
upward_growth19 分钟前
一叶障目 -- 你不知道的"最长递增子序列"!
前端·javascript·算法
半间烟雨23 分钟前
Web3(阶段一:入门)——哈希算法
算法·web3·区块链·哈希算法
@蓝莓果粒茶30 分钟前
LeetCode第131题_分割回文串
开发语言·前端·算法·leetcode·职场和发展·c#·.net
雾里看山1 小时前
算法思想之滑动窗口(一)
算法·leetcode·推荐算法
梭七y1 小时前
【力扣hot100题】(034)LRU缓存
leetcode·缓存·哈希算法
猫咪-95272 小时前
链表算法中常用操作和技巧
数据结构·算法·链表
被AI抢饭碗的人2 小时前
算法题(114):矩阵距离
算法
javaisC2 小时前
数据结构----------顺序查找,折半查找和分块查找(java实现)
java·数据结构·算法