Leetcode Binary Tree Inorder Traversal
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?
confused what "{1,#,2,3}"
means?
递归解决方案
- class Solution {
- public:
- vector<int> res;
- void inorder(TreeNode* root){
- if(root == NULL) return;
- inorder(root->left);
- res.push_back(root->val);
- inorder(root->right);
- }
- vector<int> inorderTraversal(TreeNode *root) {
- inorder(root);
- return res;
- }
- };
递归中序遍历
非递归解决方案
- class Solution {
- public:
- vector<int> inorderTraversal(TreeNode *root) {
- vector<int> res;
- if(root == NULL) return res;
- stack<TreeNode *> nodeStack;
- TreeNode *current = root;
- while(!nodeStack.empty() || current ){
- if(current != NULL){
- nodeStack.push(current);
- current = current->left;
- }else{
- current = nodeStack.top();nodeStack.pop();
- res.push_back(current->val);
- current = current->right;
- }
- }
- return res;
- }
- };
非递归中序遍历
Leetcode Binary Tree Inorder Traversal的更多相关文章
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode] Binary Tree Inorder Traversal 中序排序
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode Binary Tree Inorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
随机推荐
- Jquery.Datatables td宽度太长的情况下,自动换行
在 td 里面 加上 style="word-wrap:break-word;" 自动换行就好了,如果不想换行,可以将超出内容设为隐藏, overflow:hidden; whit ...
- SQL Server数据库大型应用解决方案总结(转载)
转载地址:http://hb.qq.com/a/20120111/000216.htm 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的互联网应用,每天百万级甚至 ...
- Algorithms, Part I by Kevin Wayne, Robert Sedgewick
Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...
- 【PHP构造方法和析构方法的使用】
构造方法:__construct,析构方法:__destruct 代码示例: <?php class Person { public $name; public $age; public fun ...
- Ajax 数据库操作
为了清楚的说明使用 Ajax 从数据库中存取信息有多么容易,我们要构建一个 MySQL 查询,然后把结果显示在ajax.html 上.但是在我们开始之前,让我们先做一些基础工作.使用下面的命令创建一个 ...
- git 常用的简单命令
git add . 会把当前目录中所有有改动的文件(不包括.gitignore中要忽略的文件)都添加到git缓冲区以待提交 git add * 会把当前目录中所有有改动的文件(包括.gitignore ...
- poj 3264 【线段树】
此题为入门级线段树 题意:给定Q(1<=Q<=200000)个数A1A2…AQ,多次求任一区间Ai-Aj中最大数和最小数的差 #include<algorithm> #incl ...
- 运行在linux上的mysql常用命令
mysql的注释:--或者# 1.mysql服务进程的命令 service mysqld start;#启动mysql服务 service mysqld status;#查看服务状态 service ...
- ZOOKEEPER3.3.3源码分析(四)对LEADER选举过程分析的纠正
很抱歉,之前分析的zookeeper leader选举算法有误,特此更正说明. 那里面最大的错误在于,leader选举其实不是在大多数节点通过就能选举上的,这一点与传统的paxos算法不同,因为如果这 ...
- LoadRunner参数化之数据取值和更新方式
其实看LR已经很久了,每次看到参数化的取值更新时,都没有看透,了解个大概就为止了,也确实挺搞脑子的. 现在理解下来 分成2部分 取值方式 Select next row 如何从数据列表中取值 Seq ...