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

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. struct node {
  9. int data;
  10. struct node *left, *right;
  11. node() : data(), left(NULL), right(NULL) { }
  12. node(int d) : data(d), left(NULL), right(NULL) { }
  13. };
  14.  
  15. void print(node *root) {
  16. stack<node*> S;
  17. if (!root) return;
  18. node *cur = root;
  19. while () {
  20. if (cur) {
  21. S.push(cur);
  22. cur = cur->left;
  23. }
  24. else if (!S.empty()) {
  25. cur = S.top();
  26. S.pop();
  27. cout << cur->data << " ";
  28. cur = cur->right;
  29. }
  30. else break;
  31. }
  32. }
  33.  
  34. int main() {
  35. struct node* root = new node();
  36. root->left = new node();
  37. root->right = new node();
  38. root->left->left = new node();
  39. root->left->right = new node();
  40. print(root);
  41. return ;
  42. }

Data Structure Binary Tree: Inorder Tree Traversal without Recursion的更多相关文章

  1. Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!

    http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ #include &l ...

  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. java 页面错误转发提示页面 errorPage转跳报HTTP500内部服务器错误

    errorPage和isErrorPage本来是很简单的功能,但是我却没弄出来,还百度了半天,结果发现是IE的设置问题.将下图中的“Show friendly HTTP error messages( ...

  2. Android_Fragment_Fragment详解

      Android_Fragment_Fragment详解 分类: Android基础2013-10-03 08:23 92人阅读 评论(0) 收藏 举报 AndroidFragmentFragmen ...

  3. mfs客户端挂载

    1.安装fuse yum install fuse fuse-devel 2.加载fuse模块 modprobe fuse 3.创建mfs用户 useradd mfs -s /sbin/nologin ...

  4. cache和buffer区别探讨

    一. 1.Buffer(缓冲区)是系统两端处理速度平衡(从长时间尺度上看)时使用的.它的引入是为了减小短期内突发I/O的影响,起到流量整形的作用.比如生产者——消费者问题,他们产生和消耗资源的速度大体 ...

  5. 获取、增加、修改、删除sqlserver字段描述及快速查看表字段与描述

    先看添加与删除字段描述 EXEC sys.sp_addextendedproperty @name = N'MS_Description', --添加Type字段说明 @value = N'屏蔽类型对 ...

  6. JS 手势长按代码

    同时支持长按和点击事件,无依赖版 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  7. appearance 设置公共属性

    //设置公共TabBarItem 的属性 [UITabBarItem appearance] //设置公共NavigationBar 的属性 [UIBarButtonItem appearance]

  8. 使用mescroll实现上拉加载与下拉刷新

    现在上拉加载与下拉刷新几乎已经是移动端必备功能之一了,自己实现一个太麻烦,但是好用的插件又非常少.之前看到网上很多人都在用iScroll,于是也尝试用它做了几个DEMO,但或多或少都有一些问题,比如这 ...

  9. 下载yum安装的rpm包

    方法一 通过yum自带的yumdownloader工具进行下载,这个工具是由yum-utils这个软件安装生成的 执行如下命令,检查yum-utils是否存在: rpm -qa |grep yum-u ...

  10. ffmpeg保存原始数据PCM YUV

    保存yuv ffmpeg -i video.mp4 -c:v rawvideo -pix_fmt yuv420p out.yuv 保存pcm ffmpeg -i input.flv -f s16le ...