1. Binary Tree Inorder Traversal My Submissions QuestionEditorial Solution

    Total Accepted: 123484 Total Submissions: 310732 Difficulty: Medium

    Given a binary tree, return the inorder traversal of its nodes’ values.

For example:

Given binary tree {1,#,2,3},

1

\

2

/

3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

思路:1.递归

2.迭代

class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> v;
if(root==NULL)return v;
vector<int> vl,vr;
vl = inorderTraversal(root->left);
v.push_back(root->val);
vr = inorderTraversal(root->right);
int n = vl.size()+v.size()+vr.size();
vector<int> res(n);
copy(vl.begin(),vl.end(),res.begin());
copy(v.begin(),v.end(),res.begin()+vl.size());
copy(vr.begin(),vr.end(),res.begin()+vl.size()+v.size());
return res;
}
};

以下是迭代方法,多练习以写出一个精简的迭代代码

思路找到左线压入栈,自底向上访问,有右子树的节点跳至右子树重复

上述过程。

class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> v;
stack<TreeNode*> st;
while(root||!st.empty()){//第一个处理root情况
while(root!=NULL){ //一直找到最左下角,没有的话后面弹出该元素值
st.push(root);
root = root->left;
}
root = st.top();
st.pop();
v.push_back(root->val);//弹出值,转至右子树
root = root->right;
}
return v;
}
};

61. Binary Tree Inorder Traversal的更多相关文章

  1. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

  2. LintCode Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  3. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  4. 3月3日(4) Binary Tree Inorder Traversal

    原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...

  5. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  6. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  7. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  8. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  9. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

随机推荐

  1. 开关电源(DC-DC)与LDO电源的区别---纹波

    https://blog.csdn.net/edadoc2013/article/details/78435775

  2. binary-tree-preorder-traversal leetcode C++

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  3. hdu 5102 The K-th Distance (队列+生成法,,)

    题意: N个点的一棵树.定义点u和点v的距离等于它们之间的路径(唯一的)的长度.这样我们可以得到n*(n-1)/2个距离. 将它们从小到大排序,问前K个数的和是多少. 思路: 将边长为1的树枝都入队列 ...

  4. Docker 18.03导入导出

    docker中分容器和镜像,简单可以理解为容器是运行中的实例,镜像是运行实例所需的静态文件. 导入导出既可以对容器做操作,也可以对镜像做操作.区别在于镜像可以随时导出,容器必须要停止之后才可以导出,否 ...

  5. Treevalue(0x02)——函数树化详细解析(上篇)

    本文将对 func_treelize 这一treevalue库中的核心功能进行详细的原理解析. 关于treevalue的概述,可以参考之前的文章:Treevalue(0x01)--功能概述 树化函数基 ...

  6. 计算机网络漫谈之IP数据包

    网络层从 网络层 .IP与子网掩码 前前后后我们也说了两次了,IP 这个东西絮絮叨叨的也一直在提.今天我们来解开IP协议的面纱,还记得我们之前在数据链路层说的物理帧的结构吗?就是这样: 其中Head叫 ...

  7. C++ pragma once 与 ifndef 用法区别

    #pragma once 与 #ifndef 的作用 (1)在C/C++中,在使用预编译指令#include的时候,为了防止重复引用造成二义性. (2)在能够支持这两种方式的编译器上,二者并没有太大的 ...

  8. Django笔记&教程 3-3 模板常用语法

    Django 自学笔记兼学习教程第3章第3节--模板常用语法 点击查看教程总目录 本文主要参考:https://docs.djangoproject.com/en/2.2/ref/templates/ ...

  9. Typora下载安装教程

    Typoa下载和安装 Typora---程序员记事本!!! 这里我们选择Typora作为我们的编辑器,功能的强大需要各位自己去体会. Typora下载地址 点击链接打开,然后选择Download! 根 ...

  10. 本机不安装Oracle客户端,使用instantclient_11_2和PLSQL Developer连接Oracle远程数据库步骤

     前言:由于Orcale客户端,占用空间太大,我们选择安装installclient\PLSQL客户端对orcale进行数据库连接. 安装要求.installClient要与PLSQL的安装位数一致, ...