http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void print(node *root) {
if (!root) return;
node *cur = root;
while (cur) {
if (!cur->left) {
cout << cur->data << " ";
cur = cur->right;
}
else {
node *pre = cur->left;
while (pre->right && pre->right != cur) pre = pre->right;
if (pre->right == NULL) {
pre->right = cur;
cur = cur->left;
}
else {
pre->right = NULL;
cout << cur->data << " ";
cur = cur->right;
}
}
}
} void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} int main() {
node* root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
print(root);
return ;
}

Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!的更多相关文章

  1. Data Structure Binary Tree: Inorder Tree Traversal without Recursion

    http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/ #include <iostream> #in ...

  2. Data Structure Binary Tree: Iterative Postorder Traversal

    http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...

  3. Data Structure Binary Tree: Morris traversal for Preorder

    http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...

  4. Data Structure Binary Tree: Boundary Traversal of binary tree

    http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/ #include <iostream> #include & ...

  5. Data Structure Binary Tree: Populate Inorder Successor for all nodes

    http://www.geeksforgeeks.org/populate-inorder-successor-for-all-nodes/ #include <iostream> #in ...

  6. Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals

    http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/ #include < ...

  7. Data Structure Binary Tree: Level order traversal in spiral form

    http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/ #include <iostream> #includ ...

  8. Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree

    struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...

  9. Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合

    给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素. 有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归 Given a binary tree, retur ...

随机推荐

  1. mysql返回记录的ROWNUM(转)

      set @rownum = 0; select (@rownum := @rownum + 1) as rownum, name, scores from user order by scores ...

  2. Android轻量级日志管理框架

    代码地址如下:http://www.demodashi.com/demo/12134.html ViseLog Android 轻量级日志框架,使用森林对象维护不同的日志树进行日志输出,可以是Logc ...

  3. iOS项目开发之实现无限轮播

    简介 分析 实现 代码下载 一.简介 在实际的开发当中,会经常有界面需要实现图片的无限轮播这样的需求.比如新闻app,或者其他app的广告位 实现的方式有很多种,最先想动的一定是scrollView, ...

  4. java统计中英文字数 Java问题通用解决代码

    http://yangchao20020.blog.163.com/blog/static/483822472011111635424751/   这个不适用于新浪微博字数的统计,结果有差别,若需要可 ...

  5. maven nexus myeclipse 学习

    http://b-l-east.iteye.com/blog/1246482 这篇文章比较详细的介绍了 nexus 本地仓库以及与maven的配合使用 http://blog.csdn.net/arv ...

  6. sqlplus登入和plsql登入的差别

    以下是两种登入方式的截图.用sqlplus登入须要输入主机字: 假设是用本机的SQL*Plus连接本机的数据库.则"主机字符串"能够为空. 假设是从远程连接xp的oracle数据库 ...

  7. Javascript属性constructor/prototype的底层原理

    在Javascript语言中,constructor属性是专门为function而设计的,它存在于每个function的prototype属性中. 这个constructor保存了指向function ...

  8. php SSL certificate problem: unable to get local issuer certificate

    加上 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); 就可以了 百度语音的demo: <?php header("Content-type ...

  9. 炒美股史考特(Scottrade)开户准备及如何获取免费交易(最新2017版)

    最新美股史考特(Scottrade)开户及汇款攻略 (2017 年 6 月) 一   前言 二   开户流程 三    激活账户 四 转账汇款 五 小结 一 前言:为什么选择史考特(Scottrade ...

  10. Java中HashTable和HashMap的区别

    在Java中,HashTable和HashMap都是哈希表,那么它们有什么区别呢?   1.它们所继承的类不一样. HashTable和HashMap都实现了Map接口,但是它们所继承的类时不同的.H ...