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;
    }
};
相关推荐
爱吃土豆的程序员18 分钟前
Lucene 倒排索引原理详解:深入探讨相关算法设计
java·算法·elasticsearch·全文检索·lucene
一个通信老学姐1 小时前
专业120+总分400+中国科学技术大学843信号与系统考研经验中科大电子信息通信工程,生物医学工程,苏医工,真题,大纲,参考书。
考研·信息与通信·信号处理
4v1d1 小时前
从决策树到GBDT、随机森林
算法·决策树·随机森林
顾城猿2 小时前
9.25-编程刷题
数据结构·算法·leetcode
王哈哈嘻嘻噜噜2 小时前
c语言中的杨氏矩阵的介绍以及元素查找的方法
c语言·算法·矩阵
大柏怎么被偷了2 小时前
【C++算法】栈
开发语言·c++·算法
小柯J桑_2 小时前
史上最牛排序集合,带你认清所有排序算法!(必看系列)~
数据结构·算法·排序算法
无限大.3 小时前
c语言200例 64
c语言·数据结构·算法
luluvx3 小时前
LeetCode[中等] 78.子集
算法·leetcode·职场和发展
sp_fyf_20243 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-26
人工智能·深度学习·神经网络·算法·语言模型·自然语言处理·数据挖掘