1+
2+ // recursion method.
3+
4+ class Solution {
5+ public:
6+ void inorder (TreeNode *root, vector<int > & result){
7+ if (root != NULL ){
8+ inorder (root->left , result);
9+ result.push_back (root -> val);
10+ inorder (root -> right, result);
11+ }
12+ }
13+ vector<int > inorderTraversal (TreeNode *root) {
14+ vector<int > result;
15+ inorder (root, result);
16+ return result;
17+ }
18+ };
19+ // iteration method
20+ // idea: inorder: left, root, right.
21+ // we push all the left node into the stack the most left one at the top of the stack
22+ // then pop it and go check the right side of the node
23+ class Solution {
24+ public:
25+
26+ vector<int > inorderTraversal (TreeNode *root) {
27+ vector<int > result;
28+ const TreeNode *p = root;
29+ stack<const TreeNode *> cache;
30+ while (p != NULL || !cache.empty ()){
31+ if ( p != NULL ){
32+ cache.push (p);
33+ p = p -> left;
34+ }else {
35+ p = cache.top ();
36+ cache.pop ();
37+ result.push_back (p->val );
38+ p = p -> right;
39+ }
40+ }
41+ return result;
42+ }
43+ };
44+ // PREORDER TRAVERSAL
45+ // an empty stack
46+ // do the following steps when the stack is not empty
47+ // push root into the stack
48+ // push right child of popped item to the stack
49+ // push left child of popped item to the stack
50+
51+ vector<int > preorderTraversal (TreeNode *root) {
52+ vector<int > result;
53+ TreeNode *p = new TreeNode (-1 );
54+ stack<TreeNode *> myStack;
55+ myStack.push (root);
56+ while (!myStack.empty ()){
57+ p = myStack.top ();
58+ result.push_back (p -> val);
59+ myStack.pop ();
60+ if (p -> right) myStack.push (p -> right);
61+ if ( p -> left) myStack.push ( p -> left);
62+ }
63+ return result;
64+ }
65+
66+
67+ // POSTORDER TRAVERSAL
68+ // Two methods: 1. two stacks 2. one stack
69+ // 1. create two stacks
70+ // 2. push root to the first stack
71+ // repeat the actions until first stack is empty
72+ // 3. pop the root and push into second stack
73+ // 4. push the left and right children of popped item into the first stack
74+ vector<int > postorderTraversal (TreeNode *root) {
75+ vector<int > result;
76+ if ( root == NULL ) return ;
77+ stack<TreeNode *> myStack1;
78+ stack<TreeNode *> myStack2;
79+ myStack1.push (root);
80+ while (!myStack1.empty ()){
81+ TreeNode *p = myStack1.top ();
82+ result.push_back (p->val );
83+ myStack1.pop ();
84+ myStack2.push (p);
85+ if ( p -> right) myStack1.push (p -> right);
86+ if ( p -> left) myStack1.push (p -> left);
87+ }
88+ std::reverse (result.begin (), result.end ());
89+ return result;
90+ }
91+ // one stack method
92+ // push each node twice and compare each time the top of the stack and curr value
93+
94+ vector postorderTraversal (TreeNode *root) {
95+ vector<int > result;
96+ if ( root == NULL ) return ;
97+ stack<TreeNode *> myStack;
98+ myStack.push (root);
99+ myStack.push (root);
100+ while (!myStack.empty ()){
101+ TreeNode *curr = myStack.top ();
102+ myStack.pop ();
103+ if (!myStack.empty () && curr -> val == myStack.top () -> val){
104+ if ( curr -> right){
105+ myStack.push ( curr -> right);
106+ myStack.push ( curr -> right);
107+ }
108+ if (curr -> left){
109+ myStack.push ( curr -> left);
110+ myStack.push ( curr -> left);
111+ }
112+ }else {
113+ result.push_back (curr -> val);
114+ }
115+ }
116+ return result;
117+ }
118+
0 commit comments