(二叉树 递归) leetcode94. Binary Tree Inorder Traversal
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的更多相关文章
- LeetCode94. Binary Tree Inorder Traversal
题目 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 考点 stack ...
- LeetCode94 Binary Tree Inorder Traversal(迭代实现) Java
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
随机推荐
- AFO && OI回忆录
技不如人,甘拜下风 今天是2019.4.6,联考第一天,菜鸡attack原题爆炸(其实是都不会)心灰意冷(其实并没有很难过)写下了这篇文章 T1 2h写个跟\(k\)无关的假算法写到最后发现是三个lo ...
- 《.NET 进阶指南》读书笔记2------定义不可改变类型
不可改变对象的定义 一个类型的对象在创建后,它的状态就不能再改变,知道它死亡,它的状态一直维持与创建时相同.这时候称该对象具有不可改变性.这样的类型为不可改变类型. 不可改变对象在创建的时候,必须完全 ...
- UDK Stat命令
Stat命令(chs en)提供了游戏和引擎各个方面的实时统计信息,输入不同参数会在屏幕HUD上显示对应统计数据. 非Shipping版的UDK才会启用STATS宏,统计逻辑才会编译进exe,才能使 ...
- spring boot跨域问题
跨域是指不同域名之间相互访问.跨域,指的是浏览器不能执行其他网站的脚本.它是浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制.也就是如果在A网站中,我们希望使用Ajax来获得B网站 ...
- 进行Spark,Kafka针对Kerberos相关配置
1. 提交任务的命令 spark-submit \--class <classname> \--master yarn \--deploy-mode client \--executor- ...
- [POI2015]PUS
嘟嘟嘟 这题只要往正确的方面想,就很简单. 首先,这是一道图论题! 想到这,这题就简单了.对于两个数\(i\)和\(j\),如果\(i\)比\(j\)大,就从\(i\)向\(j\)连边.然后如果图中存 ...
- 对var的新笔记
今天看阮老师的ES6入门时,看见一个对我来说从没想到过的var赋值变量导致的错误,故记录一下 var tmp = new Date(); function f() { console.log(tmp) ...
- C语言之将弧度值转换为角度值
#include<stdio.h> #include<stdlib.h> #define pi 3.141592 void conver_radian(float radian ...
- xml错误之cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
今天从svn导入项目的时候,一个xml文件里面报错:‘cvc-complex-type.2.4.c: The matching wildcard is strict, but no declarati ...
- Linux(Ubuntu)使用日记------markdown文件与pdf,doc,docx文件的相互转化(pandoc使用)
安装: sudo apt-get install pandoc 使用: man pandoc 查看帮助文档 直接转换,命令如下: pandoc -f markdown -t docx ./test ...