Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!
2014-03-28 10:55
447 查看
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(0), 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(1); root->left = new node(2); root->right = new node(3); root->left->left = new node(4); root->left->right = new node(5); print(root); return 0; }
相关文章推荐
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
- Inorder Tree Traversal without recursion and without stack!
- Inorder Tree Traversal without recursion and without stack!
- Inorder Tree Traversal without recursion and without stack!
- [Leetcode]Binary Tree Inorder Traversal without recursion
- Data Structure Binary Tree: Level order traversal in spiral form
- Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal (给出前序中序求二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal 分类: leetCode 2015-06-22 08:26 177人阅读 评论(0) 收藏
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode刷题【Array】 Construct Binary Tree from Preorder and Inorder Traversal
- 树 - Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
- 【leetocde】 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium) (cpp)
- LeetCode-106.Construct Binary Tree from Inorder and Postorder Traversal