原题解

题目

约束

题解

方法一

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. 二叉树的中序遍历()的更多相关文章

  1. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  2. Java实现 LeetCode 94 二叉树的中序遍历

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...

  3. Leetcode 94. 二叉树的中序遍历

    1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...

  4. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...

  5. leetcode 94二叉树的中序遍历

    递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  6. 【leetcode 94. 二叉树的中序遍历】解题报告

    前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...

  7. LeetCode 94 ——二叉树的中序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...

  8. 【LeetCode】94. 二叉树的中序遍历

    94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...

  9. Leetcode题目94.二叉树的中序遍历(中等)

    题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...

  10. leetcode刷题-94二叉树的中序遍历

    题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...

随机推荐

  1. Oracle 存储过程5:PL/SQL异常处理

    PL/SQL异常处理是PL/SQL块中对执行部分出现异常进行处理的部分.PL/SQL采用的是统一异常处理机制,当异常发生时,程序会自动跳转到异常处理部分,交给异常处理程序进行异常匹配,再调用对应的处理 ...

  2. Python3 学习基础知识

    python是动态语言(对象属性可以动态改变,删除添加..),不是强类型语言,所以和java,c/c++等强类型静态语言有不一样地方需要注意. 一:基本数据类型 变量 counter = 1   # ...

  3. OCR开源项目

    转发自https://www.toutiao.com/a6802032805313249806/?tt_from=weixin&utm_campaign=client_share&wx ...

  4. 打卡node day01--基础和fs内置模块

    一, 检查 node 版本 node -v 或 node --version 二,检查安装过的node 版本(nvm node 版本管理工具  安装链接 参考百度) nvm ls 三,切换node 版 ...

  5. Java流程控制之Scanner的进阶使用

    Scanner的进阶使用 import java.util.Scanner; public class Demo04 { public static void main(String[] args) ...

  6. Python 字典类型

    1.由于字典中的 key 是非常关键的数据,而且程序需要通过 key 来访问 value,因此字典中的 key 不允许重复.程序既可使用花括号语法来创建字典,也可使用 dict() 函数来创建字典.实 ...

  7. Angular前端调用asp.net core web api后端,报跨域问题

    在 ASP.NET Core 中启用跨域请求 (CORS) https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnet ...

  8. python查看服务器cpu、硬盘、内存使用率,用于日常巡检

    最近由于增加了很多新的服务,服务器经常会因为oom.磁盘空间不足等原因造成各种各样的问题.所以需要写一个小工具完成对各服务器的巡检. 思路比较简单:利用paramiko这个库,在服务器上执行linux ...

  9. Linux C语言编程基础

    Linux C语言编程基础 选择教材第二章的一节进行编程基础练习 二叉树广度优先遍历(链队) 算法: "head.h" #ifndef _head_h_ #define _head ...

  10. 杭电OJ--1048-C++实现

    #include <iostream>#include<vector>#include<string>#include<cctype>#include& ...