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

Example:

Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]

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

---------------------------------------------------------------------------------------------------------------

leetcode 144. Binary Tree Preorder Traversal 几乎相似。题目要求我们尽量用迭代求解,不过,我不太会,所以用递归。

C++代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> vec;
inorder(root,vec);
return vec;
}
void inorder(TreeNode *root,vector<int> &vec){
if(!root) return;
inorder(root->left,vec);
vec.push_back(root->val);
inorder(root->right,vec);
}
};

(二叉树 递归) leetcode94. Binary Tree Inorder Traversal的更多相关文章

  1. LeetCode94. Binary Tree Inorder Traversal

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

  2. LeetCode94 Binary Tree Inorder Traversal(迭代实现) Java

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...

  3. Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)

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

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

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

  5. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

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

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

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

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

  8. 49. leetcode 94. Binary Tree Inorder Traversal

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

  9. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

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

随机推荐

  1. Vue开发插件

    (一)Vue.js的插件应该有一个公开方法:install. 这个方法的第一个参数是Vue构造器,第二个参数是一个可选的选项对象,一般是如下操作: MyPlugin.install = functio ...

  2. 教你一步永久激活WebStorm2018

    工欲善其事必先利其器,我们在开发过程中,编辑器是我们提高开发效率及生产必备的工具,如何发现一个高效好用的编辑器是程序员必备的技能之一. 前端开发有众多编辑器 sublime.vscode.webstr ...

  3. 记录Nginx作为静态资源web服务场景配置

    server { listen   80; server_name    localhost; sendfile    on; access_log    /var/log/nginx/host.ac ...

  4. Thermostat:双层存储结构的透明巨页内存管理机制

    这是一篇由密歇根大学的Neha Agarwal 和 Thomas F. Wenisch,发表在计算机系统顶会ASLOS的论文,Thermostat: Application-transparent P ...

  5. Quick Select算法

    https://blog.csdn.net/Yaokai_AssultMaster/article/details/68878950 https://blog.csdn.net/mrbcy/artic ...

  6. 是时候选择一款富文本编辑器了(wangEditor)

    需要一款富文本编辑器,当然不能自己造轮子.本来想使用cnblog也在用的TinyMCE,名气大,功能全.但是发现TinyMCE从4.0开始,不再支持直接下载.所以还是决定选用wangEditor.遗憾 ...

  7. centos7 安装 pyspider 出现的一系列问题及解决方案集合

    先安装python3 和 pip3 wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz 安装zlib-devel包(后面安装pi ...

  8. IdentityServer4客户端如何获取自定义声明,了解一下?

    前言 久违了各位,之前录制过IdentityServer4的基础视频(https://space.bilibili.com/319652230/#/),有兴趣了解的童鞋可以看一下,只不过未发表成博客. ...

  9. openstack安装过程报错

    问题一 .执行启动neutron服务报错[root@localhost ~]# systemctl start neutron-server.service Job for neutron-serve ...

  10. 四十二、在线预览pdf文件

    //文档在线观看 checkWoc(type, id, taskId, smsId, stsId) { if(type == "zip" || type == "7z&q ...