LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal

Title Description

144. Binary Tree Preorder Traversal
94. Binary Tree Inorder Traversal
145. Binary Tree Postorder Traversal

Preamble arrangement: Root - Left - Right

Are arranged in order: left - Root - Right

After the sequence alignment: left - right - Root

 

Answers

 1 // PreOrder
 2 /**
 3  * Definition for a binary tree node.
 4  * struct TreeNode {
 5  *     int val;
 6  *     TreeNode *left;
 7  *     TreeNode *right;
 8  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 9  * };
10  */
11 class Solution {
12 private:
13         vector<int> res;
14 
15 public:
16     vector<int> preorderTraversal(TreeNode* root) {
17         foo(root);
18         return res;
19     }
20     void foo(TreeNode* root){
21         if(root == NULL)
22             return;
23         
24         res.push_back(root->val);
25         foo(root->left);
26         foo(root->right);
27     }
28 };
29 
30 // InOrder
31 class Solution {
32 public:
 33 is      Vector < int > inorderTraversal (the TreeNode * the root) {
 34 is          IF (the root == NULL) {
 35              return Vector < int > ();
 36          }
 37 [          Vector < int > Result; // Create a result 
38 is          Stack <* the TreeNode> S; // stack creation 
39          TreeNode * p = root; // temporary so that p is the root
 40          // find node p leftmost 
41          the while (p =! NULL) {
 42              s.push (p); //Starting from the root, simply push the left node stack 
43 is              p = p-> left; // update p to the left of node 
44 is          }
 45          // s to all nodes in the left stack
 46          // to loop through s 
47          the while ( ! s.empty ()) {
 48              // the most left, pushing Stack 
49              P = s.top ();
 50              result.push_back (p-> Val);
 51 is              s.pop (); // own He disappeared
 52              // then observe the right Node 
53 is              IF (p-> right =! NULL) {
 54 is                  P = p-> right;
55                  the while (P = NULL!) { // observation left node 
56 is                      s.push (P);
 57 is                      P = p-> left;
 58                  }
 59              }     
 60          }
 61 is          return Result;
 62 is      }
 63 is  };
 64  
65  // postorder 
66  
67  class Solution {
 68  public :
 69      Vector < int > postorderTraversal (the TreeNode * the root) {
 70          IF (the root == NULL){
71             return vector<int>();
72         }
73         vector<int> result;
74         stack<TreeNode *> s;
75         
76         s.push(root);
77         while(!s.empty()){
78             TreeNode *temp = s.top();
79             result.push_back(temp->val);
80             s.pop();
81             if(temp->left!=NULL){
82                 s.push(temp->left);
83             }
84             if(temp->right!= NULL){
85                 s.push(temp->right);
86             }
87         }
88         reverse(result.begin(), result.end());
89         return result;
90         
91     }
92 };

 

 

Guess you like

Origin www.cnblogs.com/kykai/p/11618819.html