Data Structure Binary Tree: Iterative Postorder Traversal
2014-03-29 10:16
387 查看
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/
#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) { if (!root) return; stack<node*> S; do { while (root) { if (root->right) S.push(root->right); S.push(root); root = root->left; } root = S.top(); S.pop(); if (root->right && !S.empty() && root->right == S.top()) { S.pop(); S.push(root); root = root->right; } else { cout << root->data << " "; root = NULL; } } while (!S.empty()); } 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); prints(root); return 0; }
相关文章推荐
- Data Structure Binary Tree: Level order traversal in spiral form
- Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
- Data Structure Binary Tree: Morris traversal for Preorder
- Binary Tree Post-Order Traversal Iterative Solution
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
- LeetCode Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- [LeetCode] Binary Tree Postorder Traversal
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中后序构造BST
- leetcode:二叉树之Binary Tree Postorder Traversal
- leetcode:二叉树之Construct Binary Tree from Inorder and Postorder Traversal
- Binary Tree Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Binary Tree Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- FTPrep, 106 Construct Binary Tree from Inorder and Postorder Traversal