[LintCode] 二叉树的中序遍历
The recursive solution is trivial and I omit it here.
Iterative Solution using Stack (O(n) time and O(n) space):
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
vector<int> nodes;
TreeNode* node = root;
stack<TreeNode*> toVisit;
while (node || !toVisit.empty()) {
if (node) {
toVisit.push(node);
node = node -> left;
}
else {
node = toVisit.top();
toVisit.pop();
nodes.push_back(node -> val);
node = node -> right;
}
}
return nodes;
}
};
Another more sophisticated soltuion using Morris Traversal (O(n) time and O(1) space):
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
vector<int> nodes;
TreeNode* node = root;
while (node) {
if (node -> left) {
TreeNode* predecessor = node -> left;
while (predecessor -> right && predecessor -> right != node)
predecessor = predecessor -> right;
if (!(predecessor -> right)) {
predecessor -> right = node;
node = node -> left;
}
else {
predecessor -> right = NULL;
nodes.push_back(node -> val);
node = node -> right;
}
}
else {
nodes.push_back(node -> val);
node = node -> right;
}
}
return nodes;
}
};
[LintCode] 二叉树的中序遍历的更多相关文章
- lintcode:二叉树的中序遍历
题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
- LeetCode(94):二叉树的中序遍历
Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- LintCode-67.二叉树的中序遍历
二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
随机推荐
- 在做和sap系统集成的一点心得
最这一个月和sap做集成,把主数据中的数据写入到sap系统中,做集成就离不开联调,平时开发中,一个复杂点的系统还容易出问题,涉及到两个系统,由于两个或多个系统来自不同的厂家,各个厂家的开发对业务的理解 ...
- JavaScript点击按钮显示 确认对话框
//JavaScript点击按钮显示确认对话框 <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...
- 【百度地图API】怎样制作多途经点的线路导航——驾车篇
摘要: 休假结束,酸奶小妹要从重庆驾车去北京.但是途中要去西安奶奶家拿牛奶饼干呢! 用百度地图API,能不能帮我实现这个愿望呢? ----------------------------------- ...
- centos7下安装openvpn,访问内网服务器 (三)证书取消授权
1.创建临时证书 使用easy-rsa创建额外的证书: [root@origalom openvpn]# cd /usr/share/easy-rsa/2.0/ [root@origalom 2.0] ...
- XP win2003系统 微软雅黑字体的使用方法
微软雅黑是微软公司为其新一代操作系统Vista开发的中文字体,据说它将是迄今为止个人电脑上可以显示的最清晰的中文字体. 微软公司表示,在新一代操作系统中为了能够更加清晰的显示文字,目前正在 ...
- git教程,切换分支并拉去
进了目录切换分支: git checkout 5.1 切换完成后拉去分支为: git pull https://www.liaoxuefeng.com/wiki/0013739516305929606 ...
- mysql 常用功能
一.备份 mysqldump [OPTIONS] database [tables] http://www.blogjava.net/Alpha/archive/2007/08/10/135694.h ...
- Nginx日志过滤 使用ngx_log_if不记录特定日志
ngx_log_if是Nginx的一个第三方模块.它在Github上的描述是这样介绍的:ngx_log_if是一个独立的模块,允许您控制不要写的访问日志,类似于Apache的"CustomL ...
- poj Sudoku(数独) DFS
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13665 Accepted: 6767 Special ...
- iOS关于版本更新的问题
// 获取app版本 NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBun ...