Data Structure Binary Tree: Morris traversal for Preorder
2014-03-29 07:02
441 查看
http://www.geeksforgeeks.org/morris-traversal-for-preorder/
#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <string> #include <fstream> #include <map> 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 prints(node *root) { while (root) { if (root->left == NULL) { cout << root->data << " "; root = root->right; } else { node* cur = root->left; while (cur->right && cur->right != root) cur = cur->right; if (cur->right == NULL) { cout << root->data << " "; cur->right = root; root = root->left; } else { cur->right = NULL; root = 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); root->right->left = new node(6); root->right->right = new node(7); root->left->left->left = new node(8); root->left->left->right = new node(9); root->left->right->left = new node(10); root->left->right->right = new node(11); prints(root); return 0; }
相关文章推荐
- Java for LeetCode 144 Binary Tree Preorder Traversal
- Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- Data Structure Binary Tree: Populate Inorder Successor for all nodes
- Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
- Data Structure Binary Tree: Level order traversal in spiral form
- Data Structure Binary Tree: Iterative Postorder Traversal
- Array-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- <LeetCode OJ> 105. Construct Binary Tree from Preorder and Inorder Traversal
- <leetcode> 105. Construct Binary Tree from Preorder and Inorder Traversal 重建二叉树
- [Leetcode 144, Medium] Binary Tree Preorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Binary Tree Preorder Traversal
- Construct Binary Tree from Inorder and PreOrder Traversal
- [Leetcode] Binary Tree Preorder Traversal
- LeetCode | Binary Tree Preorder Traversal