当前位置:网站首页>力扣经典二叉树题目
力扣经典二叉树题目
2022-07-19 18:58:00 【锡兰_CC】
目录
144. 二叉树的前序遍历
描述:给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
解题思路:
- 1)若树为空,直接返回 NULL。
- 2)访问根结点,存下结点的值。
- 3)递归左子树。
- 4)递归右子树。
参考代码:
/**
* 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 preorder(TreeNode *root, vector<int> &ans) { //前序遍历
if (root == nullptr) return; //判断是否为空
ans.push_back(root -> val);
preorder(root -> left, ans); //递归左子树
preorder(root -> right, ans); //递归右子树
return;
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans; //结果数组
preorder(root, ans); //前序遍历
return ans; //返回
}
};
94. 二叉树的中序遍历
描述:给定一个二叉树的根节点 root
,返回 它的 中序 遍历 。
解题思路:
- 1)若树为空,直接返回 NULL。
- 2)递归左子树。
- 3)访问根结点,存下结点的值。
- 4)递归右子树。
参考代码:
/**
* 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 inorder(TreeNode* root, vector<int>& ans) {//中序遍历
if (!root) return; //判断是否为空
inorder(root->left, ans); //递归左子树
ans.push_back(root->val);
inorder(root->right, ans); //递归右子树
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans; //结果数组
inorder(root, ans); //中序遍历
return ans; //返回
}
};
145. 二叉树的后序遍历
描述:给你一棵二叉树的根节点 root
,返回其节点值的 后序遍历 。
解题思路:
- 1)若树为空,直接返回 NULL。
- 2)递归左子树。
- 3)递归右子树。
- 4)访问根结点,存下结点的值。
参考代码:
/**
* 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 postorder(TreeNode *root, vector<int> &ans) {//后序遍历
if (root == nullptr) return; //判断是否为空
postorder(root->left, ans); //遍历左子树
postorder(root->right, ans); //遍历右子树
ans.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans; //答案数组
postorder(root, ans); //后序遍历
return ans; //返回
}
};
102. 二叉树的层序遍历
描述:给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
解题思路:
- 1)若树非空,创建辅助队列,头结点入队。
- 2)将队头结点储存下来记作结点 node,记录结点数据,然后进行出队操作。
- 3)将结点 node 的左右孩子进行入队操作。
- 4)重复操作至队列为空。
参考代码:
/**
* 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) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> ans;
while (!que.empty()) {
int size = que.size();
vector<int> vec;
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
ans.push_back(vec);
}
return ans;
}
};
104. 二叉树的最大深度
描述:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
解题思路:
- 1)当遇到空结点时,返回 0。
- 2)递归计算出其左子树和右子树的最大深度。
参考代码:
/**
* 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) {
if (root == nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
边栏推荐
- 38行PHP代码免导入数据库分析Linux访问日志
- 深圳某游戏研发公司给每个工位都装监控,网友:堪比坐牢!
- Icml2022 tutorial | causal fairness analysis, 68 Pages pdf
- Opencv learning materials sharing: Chinese, graphics and text, code notes are both abundant, and it is recommended to collect them
- 6041 万、阿里云中标:北方健康《北方中心2022年云服务项目(服务器、安全设备)》
- 10年测试工程师总结分享,一文教会你怎么快速找bug以及测试用例的编写
- Pytorch——模型的读取和存储
- QQ customer service consultation scrolls with the scroll bar (right)
- Regular expression syntax table
- 60 AI pharmaceutical enterprises on the map of China
猜你喜欢
声网传输层协议 AUT 的总结与展望丨Dev for Dev 专栏
银行业数据安全建设专题分析
GEE(7):GEE插件Open Earth Engine extension提高效率
Uniapp uses local storage to transfer values between pages
2022-7-13总结
Inftnews | time, with a 99 year history, is leading traditional media into NFT
uniapp 使用本地存储实现页面间的传值
6. Look for duplicates
Pytorch——模型的读取和存储
Programming examples of stm32f1 and stm32subeide -bh1750 ambient light intensity sensor drive
随机推荐
36 亿元、对象存储市场:XSKY 7亿、华为 6.9亿、杉岩 4.6亿、新华三 3.6亿、浪潮 3.2亿、中移 3亿
5.搜索二维矩阵
Open a window and pop up a tool with favorite links
揭开,字节跳动全链路压测的实践之路
STM32开发笔记121:我理解的卡尔曼滤波
Matter统一的智能家居连接标准,赋能智能设备间本地自动化交互
Programming examples of stm32f1 and stm32subeide -bh1750 ambient light intensity sensor drive
中国三氟乙醇行业研究与投资预测报告(2022版)
STM32F1与STM32CubeIDE编程实例-BMP280气压温度传感器驱动
取得刚刚添加记录的ID
Innftnews | l'avenir de la musique sur le Web 3
【TS】初识 TypeScript
孙正义万字访谈实录:未来30年一切将被重新定义!
103.(cesium篇)cesium蜂巢图(正方形)
洛谷P3398 仓鼠找 sugar 题解
uniapp 使用本地存储实现页面间的传值
IDEA解决.properties中文乱码问题
Icml2022 tutorial | causal fairness analysis, 68 Pages pdf
Anfulai embedded weekly report no. 274: 2022.07.11--2022.07.17
Get the ID of the record just added