Binary Tree Inorder Traversal & Preorder Traversal

来源:互联网 发布:gabrielle名字 知乎 编辑:程序博客网 时间:2024/05/17 06:38

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1    \     2    /   3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

Recursive:

[cpp] view plaincopyprint?
  1. /** 
  2.  * Definition for binary tree 
  3.  * struct TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode *left; 
  6.  *     TreeNode *right; 
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
  8.  * }; 
  9.  */  
  10. class Solution {  
  11. public:  
  12.     void preorder(TreeNode* root, vector<int> &path)  
  13.     {  
  14.         if(root!=NULL)  
  15.         {  
  16.             path.push_back(root->val);  
  17.             preorder(root->left, path);  
  18.             preorder(root->right, path);  
  19.         }  
  20.     }  
  21.     vector<int> preorderTraversal(TreeNode *root) {  
  22.         // IMPORTANT: Please reset any member data you declared, as  
  23.         // the same Solution instance will be reused for each test case.  
  24.         vector<int> path;  
  25.         preorder(root, path);  
  26.         return path;  
  27.     }  
  28. };  


Iterative:

[cpp] view plaincopyprint?
  1. vector<int> preorderTraversal(TreeNode *root) {  
  2.         // IMPORTANT: Please reset any member data you declared, as  
  3.         // the same Solution instance will be reused for each test case.  
  4.         vector<int> path;  
  5.         stack<TreeNode*> stk;  
  6.         while(root != NULL || !stk.empty())  
  7.         {  
  8.             if(root != NULL)  
  9.             {  
  10.                 while(root)  
  11.                 {  
  12.                     path.push_back(root->val);  
  13.                     stk.push(root);  
  14.                     root=root->left;  
  15.                 }  
  16.             }  
  17.             else{  
  18.                 root = stk.top()->right;  
  19.                 stk.pop();  
  20.             }  
  21.         }  
  22.         return path;  
  23.     }  

[cpp] view plaincopyprint?
  1. vector<int> preorderTraversal(TreeNode *root) {  
  2.         // IMPORTANT: Please reset any member data you declared, as  
  3.         // the same Solution instance will be reused for each test case.  
  4.         vector<int> path;  
  5.         stack<TreeNode*> stk;  
  6.         if(root != NULL)  
  7.         {  
  8.             stk.push(root);  
  9.             while(!stk.empty())  
  10.             {  
  11.                 root = stk.top();  
  12.                 stk.pop();  
  13.                 path.push_back(root->val);  
  14.                 if(root->right)  stk.push(root->right);  
  15.                 if(root->left) stk.push(root->left);  
  16.             }  
  17.         }  
  18.         return path;  
  19.     }  

其他两种非递归的遍历算法:

中序遍历-非递归:

[cpp] view plaincopyprint?
  1. vector<int> InOrderTraversal(TreeNode *root) {  
  2.         // IMPORTANT: Please reset any member data you declared, as  
  3.         // the same Solution instance will be reused for each test case.  
  4.         vector<int> path;  
  5.         stack<TreeNode*> stk;  
  6.         while(root != NULL || !stk.empty())  
  7.         {  
  8.             while(root != NULL)  
  9.             {  
  10.                 stk.push(root);  
  11.                 root = root->left;  
  12.             }  
  13.             if( !stk.empty())  
  14.             {  
  15.                 root = stk.top();  
  16.                 stk.pop();  
  17.                 root = root->right;  
  18.             }  
  19.         }  
  20.   
  21.         return path;  
  22.     }  

后序遍历-非递归:

[cpp] view plaincopyprint?
  1. struct TNode{  
  2.         //TreeNode* root;  
  3.         int val;  
  4.         TNode *left;  
  5.         TNode *right;  
  6.         bool isVisited;  
  7.         TNode(int a):val(a),left(NULL),right(NULL),isVisited(false){};  
  8.     };  
  9.   
  10.     vector<int> PostOrderTraversal(TNode *root) {  
  11.         // IMPORTANT: Please reset any member data you declared, as  
  12.         // the same Solution instance will be reused for each test case.  
  13.         vector<int> path;  
  14.         stack<TNode*> stk;  
  15.         if(root != NULL)  
  16.         {  
  17.             root->isVisited = false;  
  18.             stk.push(root);  
  19.         }  
  20.         TNode* cur = NULL;  
  21.         while(!stk.empty())  
  22.         {  
  23.             cur = stk.top();  
  24.             if(cur->isVisited)  
  25.             {  
  26.                 path.push_back(cur->val);  
  27.                 stk.pop();  
  28.             }else{  
  29.                 cur->isVisited=true;  
  30.                 if(cur->right)  
  31.                 {  
  32.                     cur->right->isVisited = false;  
  33.                     stk.push(cur->right);  
  34.                 }  
  35.                 if(cur->left)  
  36.                 {  
  37.                     cur->left->isVisited = false;  
  38.                     stk.push(cur->left);  
  39.                 }  
  40.             }  
  41.         }  
  42.         return path;  
  43.     }  


0 0
原创粉丝点击