代码随想录训练营Day10 | 二叉树的遍历

news/2024/10/28 15:19:15/

递归遍历

  • 题目链接:
    1. 144.二叉树的前序遍历
   void preRecur(vector<int>& ans, TreeNode* root) {if(root) {ans.push_back(root->val);preRecur(ans, root->left);preRecur(ans, root->right);}}
  1. 145.二叉树的后序遍历
 void sufRecur(vector<int>& ans, TreeNode* root) {if(root) {sufRecur(ans, root->left);sufRecur(ans, root->right);ans.push_back(root->val);}}
  1. 94.二叉树的中序遍历
 void inRecur(vector<int>& ans, TreeNode* root) {if(root) {inRecur(ans, root->left);ans.push_back(root->val);inRecur(ans, root->right);}}

层序遍历

  • 题目链接:
    1. 144.二叉树的前序遍历
void preIter(vector<int>& ans, TreeNode* root) {stack<TreeNode*> s;s.push(root);while(!s.empty()) {TreeNode* t = s.top();ans.push_back(t->val);s.pop();if(t->right)s.push(t->right);if(t->left)s.push(t->left);}}
  1. 145.二叉树的后序遍历
 void sufIter(vector<int>& ans, TreeNode* root) {stack<TreeNode*> s;TreeNode* cur = root;TreeNode* prev = nullptr;while(cur || !s.empty()) {while(cur) {s.push(cur);cur = cur->left;}cur = s.top();s.pop();if(cur->right == nullptr || cur->right == prev) {ans.push_back(cur->val);prev = cur;cur = nullptr;} else {s.push(cur);cur = cur->right;}}}
  1. 94.二叉树的中序遍历
  void inIter(vector<int>& ans, TreeNode* root) {stack<TreeNode*> s;TreeNode* cur = root;while(cur || !s.empty()) {while(cur) {s.push(cur);cur = cur->left;}cur = s.top();s.pop();ans.push_back(cur->val);cur = cur->right;}}

二叉树的层序遍历

  • 题目链接:
    102.二叉树的层序遍历
class Solution {
public:vector<vector<int>> levelOrder(TreeNode* root) {vector<vector<int>> ans;if(!root) return ans;TreeNode* cur = nullptr;deque<TreeNode*> dq;dq.push_back(root); // 双端队列while(!dq.empty()) {vector<int> t;int n = dq.size(); // 遍历一层for(int i = 0; i < n; ++i) {cur = dq.front();dq.pop_front();t.push_back(cur->val);if(cur->left)dq.push_back(cur->left);if(cur->right)dq.push_back(cur->right);}ans.push_back(t);}return ans;}
};

107.二叉树的层次遍历II

class Solution {
public:// 思路与上题一致vector<vector<int>> levelOrderBottom(TreeNode *root) {if (root == nullptr) return {};vector<vector<int>> ans;vector<TreeNode*> cur{root};while (cur.size()) {vector<TreeNode*> nxt;vector<int> vals;for (auto node : cur) {vals.push_back(node->val);if (node->left)  nxt.push_back(node->left);if (node->right) nxt.push_back(node->right);}cur = move(nxt);ans.emplace_back(vals);}ranges::reverse(ans);return ans;}
};

199.二叉树的右视图

class Solution {vector<int> ans;void dfs(TreeNode* node, int depth) {if (node == nullptr) {return;}if (depth == ans.size()) { // 这个深度首次遇到ans.push_back(node->val);}dfs(node->right, depth + 1); // 先递归右子树,保证首次遇到的一定是最右边的节点dfs(node->left, depth + 1);}public:vector<int> rightSideView(TreeNode* root) {dfs(root, 0);return ans;}
};

637.二叉树的层平均值

class Solution {
public:vector<double> averageOfLevels(TreeNode* root) {vector<double> ans;if(!root) return ans;TreeNode* cur = nullptr;deque<TreeNode*> dq;dq.push_back(root);while(!dq.empty()) {int n = dq.size();double sum = 0;for(int i = 0; i < n; ++i) {cur = dq.front();dq.pop_front();sum += cur->val;                if(cur->left)dq.push_back(cur->left);if(cur->right)dq.push_back(cur->right);}ans.push_back(sum / n);}return ans;}
};

429.N叉树的层序遍历

class Solution {
public:vector<vector<int>> levelOrder(Node* root) {vector<vector<int>> ans;if(!root) return ans;Node* cur = nullptr;deque<Node*> dq;dq.push_back(root);while(!dq.empty()) {vector<int> t;int n = dq.size();for(int i = 0; i < n; ++i) {cur = dq.front();dq.pop_front();t.push_back(cur->val);for(Node* t : cur->children) dq.push_back(t);}ans.push_back(t);}return ans;}
};

515.在每个树行中找最大值

class Solution {
public:vector<int> largestValues(TreeNode* root) {if (!root) {return {};}vector<int> res;queue<TreeNode*> q;q.push(root);while (!q.empty()) {int len = q.size();int maxVal = INT_MIN;while (len > 0) {len--;auto t = q.front();q.pop();maxVal = max(maxVal, t->val);if (t->left)  q.push(t->left);if (t->right) q.push(t->right);}res.push_back(maxVal);}return res;}
};

116.填充每个节点的下一个右侧节点指针

class Solution {
public:Node* connect(Node* root) {if(!root) return nullptr;Node* cur = nullptr;deque<Node*> dq;dq.push_back(root);while(!dq.empty()) {int n = dq.size();for(int i = 0; i < n; ++i) {cur = dq.front();dq.pop_front();if(i != n - 1)cur->next = dq.front();  // 填充nextif(cur->left) dq.push_back(cur->left);if(cur->right) dq.push_back(cur->right);}}return root;}
};

117.填充每个节点的下一个右侧节点指针II

class Solution {
public:Node* connect(Node* root) {if(!root) return nullptr;Node* cur = nullptr;deque<Node*> dq;dq.push_back(root);while(!dq.empty()) {int n = dq.size();for(int i = 0; i < n; ++i) {cur = dq.front();dq.pop_front();if(i != n - 1)cur->next = dq.front(); // 填充nextif(cur->left) dq.push_back(cur->left);if(cur->right) dq.push_back(cur->right);}}return root;}
};

104.二叉树的最大深度

class Solution {
public:int maxDepth(TreeNode* root) {if (root == nullptr) {return 0;}int l_depth = maxDepth(root->left);int r_depth = maxDepth(root->right);return max(l_depth, r_depth) + 1; // 记录高度,记录高度最大值}
};

111.二叉树的最小深度

class Solution {int ans = INT_MAX; // 遍历时记录高度,遍历到叶子节点取高度最小值void dfs(TreeNode *node, int cnt) {if (node == nullptr) {return;}cnt++;if (node->left == node->right) { // node 是叶子ans = min(ans, cnt);return;}dfs(node->left, cnt);dfs(node->right, cnt);};
public:int minDepth(TreeNode *root) {dfs(root, 0);return root ? ans : 0;}
};

http://www.ppmy.cn/news/1542614.html

相关文章

DICOM 基础知识:深入理解DICOM数据结构与标签说明

目录 DICOM 图像概念 DICOM 图像关键特性&#xff1a; DICOM 文件结构 常见数据元素&#xff1a; 数据元素示例详解 DICOM-VR 数据类型说明 DICOM 标准支持的数据集 结语 DICOM 图像概念 DICOM&#xff08;Digital Imaging and Communications in Medicine&…

JavaEE初阶---多线程(五)---定时器/线程池介绍

文章目录 1.定时器的介绍2.线程池2.1为什么需要使用线程池2.2如何进行线程池的创建2.3普通的构造方法的局限性2.4该种对象创建的方法的特点2.5线程池的模拟实现的逻辑 3.ThreadPoolExecutor类的介绍3.1构造方法3.2四种拒绝的策略 1.定时器的介绍 下面的这个就是我们的这个定时…

前端面试题-token的登录流程、JWT

这是我的前端面试题的合集的第一篇&#xff0c;后面也会更新一些笔试题目。秋招很难&#xff0c;也快要结束了。但是&#xff0c;不要放弃&#xff0c;一起加油^_^ 一、token的登录流程 1.客户端用账号密码请求登录 2.服务端收到请求&#xff0c;需要去验证账号密码 3.验证成…

SpringSecurity + Jwt权限校验,接口调用403 Forbidden问题排查与解决

问题背景&#xff1a;部分接口调用正常&#xff0c;部分接口调用报403Forbidden&#xff0c;postman不显示具体报错信息。 问题描述&#xff1a; 接口调用报错&#xff0c;经排查&#xff0c;权限校验认证通过&#xff0c;可以进入接口&#xff0c;但是在执行过程中&#xff0…

【MySQL】C语言连接MySQL数据库3——事务操作和错误处理API

目录 1.MySQL事务处理机制 1.1.autocommit 1.2.autocommit的设置与查看 1.3.使用示例 2.事务操作API 2.1.设置事务提交模式——mysql_autocommit() 2.2.提交事务——mysql_commit() 2.3.事务回滚——mysql_rollback() 3.错误处理的API 3.1.返回错误的描述——mysql_er…

R语言 | paletteer包:拥有2100多个调色板!

看到 PMID:39024031 文章的代码中&#xff0c;有颜色设置的语句&#xff1a; pal <- paletteer_d("ggsci::category20_d3")[c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)]DimPlot(MM,reduction umap,group.by "sample",label F,pt.size 0.1,c…

小知识点的回顾

1.在正式测试前,对产品或系统的一次简单的验证性测试称为: 验收测试、集成测试、冒烟测试、负载测试(冒烟。不通过就没法测)(验收在软件产品完成了功能和非功能测试后,以用户的角度来验证软件是否满足业务需求和合同规定的要求。集成测试关注模块之间的接口,单元测试后。…

太速科技-217-A(B)-Base Camera link 转光纤传输双向模块

A&#xff08;B&#xff09;-Base Camera link 转光纤传输双向模块 一、板卡概述 本板卡为1路Cameralink图像数据转为1路光纤接口的数据转换板。具备1路Camera-Link&#xff08;BASE&#xff09;图像输入端口&#xff0c; 1路单芯单模光纤通道输出接口。或者1路光纤输入…