[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 ...
随机推荐
- SSH登陆响应慢的问题
http://xiaobin.net/201112/ssh-login-quite-slow/ 同样的问题,有可能是两种情况: 第一种情况比较常见,也有很多资料提及到,就是在SSH登陆时服务器端会对客 ...
- Linux 压缩文件的命令行总结
Linux压缩文件的读取 · *.Z compress 程序压缩的档案: · *.bz2 bzip2 程序压缩的档案: · *.gz gzip 程序压缩 ...
- SQL SERVER 如何处理带字母的自增列--【叶子】
--需求说明: /* id col ---------- ---------- AB00001 a AB00002 b --当再插入数据的时候让id自动变成AB00003 ...
- 部署NopCommerce商城系统问题整理
NopCommerce是一个很棒的开源商城系统,下面整理一下我在部署使用NopCommerce系统中的一些问题. 我使用的是NopCommerce3.9版本. 1.安装 安装教程网上很多,这里不细说, ...
- android http post 请求与 json字符串
一.目标 android客户端发送一个json格式的http的请求,期望得到服务端的一个json反馈. 1. 客户端发送的json格式为: {"data" : "valu ...
- JUC组件扩展(二)-JAVA并行框架Fork/Join(一):简介和代码示例
一.背景 虽然目前处理器核心数已经发展到很大数目,但是按任务并发处理并不能完全充分的利用处理器资源,因为一般的应用程序没有那么多的并发处理任务.基于这种现状,考虑把一个任务拆分成多个单元,每个单元分别 ...
- 未设置BufferSize导致FTP下载速度过慢的问题
開始下载前设置BufferSize就可以解决: ftpClient.setBufferSize(1024*1024); 查看commons-net的源代码.能够发现假设未设置该參数.将会一个字节一个字 ...
- Java Servlet/JSP容器配置 session id
http://www.eclipse.org/jetty/documentation/current/session-management.html#setting-session-character ...
- 【flink training】 打车热点区域实时统计PopularPlaces
http://training.data-artisans.com/是Apache Flink商业公司DataArtisans提供的一个flink学习平台,主要提供了一些业务场景和flink api结 ...
- PHP进制转换[实现2、8、16、36、64进制至10进制相互转换]
自己写了一个PHP进制转换程序,一个类吧,第一次写这个东东,写这个东东,在处理文本文件时能用得到. 可以实现: 10进制转换2.8.16.36.62进制2.8.16.36.62进制转换10进制 有 ...