LeetCode 94. 二叉树的中序遍历()
题目

约束
题解
方法一


class Solution {
public:
void inorder(TreeNode* root, vector<int>& res) {
if (!root) {
return;
}
inorder(root->left, res);
res.push_back(root->val);
inorder(root->right, res);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
inorder(root, res);
return res;
}
};
方法二


class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> stk;
while (root != nullptr || !stk.empty()) {
while (root != nullptr) {
stk.push(root);
root = root->left;
}
root = stk.top();
stk.pop();
res.push_back(root->val);
root = root->right;
}
return res;
}
};
方法三


class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode *predecessor = nullptr;
while (root != nullptr) {
if (root->left != nullptr) {
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
predecessor = root->left;
while (predecessor->right != nullptr && predecessor->right != root) {
predecessor = predecessor->right;
}
// 让 predecessor 的右指针指向 root,继续遍历左子树
if (predecessor->right == nullptr) {
predecessor->right = root;
root = root->left;
}
// 说明左子树已经访问完了,我们需要断开链接
else {
res.push_back(root->val);
predecessor->right = nullptr;
root = root->right;
}
}
// 如果没有左孩子,则直接访问右孩子
else {
res.push_back(root->val);
root = root->right;
}
}
return res;
}
};
LeetCode 94. 二叉树的中序遍历()的更多相关文章
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...
- leetcode 94二叉树的中序遍历
递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【leetcode 94. 二叉树的中序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...
- LeetCode 94 ——二叉树的中序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...
- 【LeetCode】94. 二叉树的中序遍历
94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
随机推荐
- elasticSearch(四)--结构化查询
结构化查询 1.请求体查询 GET(POST) /_search POST /_search { "from": 30, "size": 10 } 2.DSL ...
- unity task
https://blog.csdn.net/weixin_43405845/article/details/105028291
- 记录坑:Chrome谷歌浏览器最小化和页面遮挡后JS代码不稳定
问题:用定时器 setInterval()做个滚动通知的动画,浏览器最小化时,定时器 setInterval()失效了,导致滚动条重叠了 可能原因: js代码不稳定 Chrome谷歌浏览器最小化和页面 ...
- httpcanary高级版--不闪退!!!!
地址 https://wwm.lanzouw.com/iOf7Hz11s4j 密码:45of
- TODOList小黄条
TODOList http://www.yynote.cn/ 总结 windows中的神器
- zabbix 监控域名到期时间
cat userparameter_http.conf UserParameter=http_discovery,/usr/bin/python /etc/zabbix/scripts/base/ht ...
- 定制个性化echarts 仪表盘
option = { series: [ { type : "gauge", center: ["50%", "45%"], // 默认全局 ...
- 在vue js中for循环使用
在线免费图片压缩工具 前端技术站 1.for(let item of response.data.result) { 用item操作每一条数据. } item:定义的每一条的变量 response.d ...
- mqtt安装和使用
linux下 下载: wget https://www.emqx.io/downloads/broker/v3.2.1/emqx-centos7-v3.2.1.zip 解压:unzip emqx-ce ...
- 腾讯云服务器,在本地域名不能访问,IP可以访问;端口访问差异:有的地方能访问有的不能
本地域名不能访问原因是DNS地址问题,本地设置的DNS地址没法解析出腾讯云IP,所以域名访问不到,直接就用服务器IP却可以. 修改DNS可以在本地电脑上修改,只影响本机, 按下键盘上的"Wi ...
