原题解

题目

约束

题解

方法一

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. ValueError: Unable to determine SOCKS version from socks

    unset all_proxy && unset ALL_PROXY export all_proxy="socks5://127.0.0.1:1080" 参考: ...

  2. h5:vue3 + ts + vite + vuex + axios + vant4 + scss + postcss+mockjs+element-plus

    模板地址:https://gitee.com/zhang_meng_lei/mobile-template-h5-vue3/tree/master 1.安装element-plus:yarn add ...

  3. XML报文解析

    /** * XML报文解析 * @param docStr */ private Map<String, Object> analysisXmlStr(String xmlStr) { t ...

  4. mysql使用保留字导致该列查不出来(mysql版本问题)

    mysql版本是 问题: 如图这边groups是sql的保留字此时这样查询是查不出来的,并会报错语法错误.但是在5.几的mysql版本中这行sql就没有问题. 解决方法: 在groups列上加上'gr ...

  5. usbip:(一)初识

    USB/IP 总结   一.概要: USB/IP项目的目标是为用户提供通过网络远程访问usb设备的能力.在用户看来,访问usb设备和访问插入到本地电脑的设备没什么区别,该项目是开源的,可以用过适当的配 ...

  6. Test Fixture框架结构

    Test Fixture框架 1.结构: setup() testcase() teardown() 2.新建unittest文件,命名unittestdemo.py 1 import unittes ...

  7. 【相关杂项】stdio.h中的sprintf函数/union的作用

    1.定义int sprintf(char *str, const char *format, ...)         1.paras:*str:目标字符串首指针  *format:要写入目标字符串的 ...

  8. linux 中的errno 和 strerror(errno)

    1. errno.h 中包含 errno 这个错误保存值 string.h 包含 strerror() 函数 ,它的原型 是 char *strerror(int errnum); 输入值应该是err ...

  9. CSR生成证书与签发证书

    openSSL生成证书 生成根证书 // Generate CA private key openssl genrsa -out ca.key 2048 // Generate CSR openssl ...

  10. Linux(CentOS7)中安装Docker

    Linux(CentOS7)中安装Docker 什么是Docker? Docker是一个开源项目, 诞生于2013年初,最初是dotCloud公司内部的一个业余项目.它基于Google公司推出的Go语 ...