树的基本操作的非递归实现

树结点

 
 
  1. struct TreeNode { 
  2.  
  3.         int val; 
  4.         struct TreeNode *left; 
  5.         struct TreeNode *right; 
  6.         TreeNode(int x):val(x),left(NULL),right(NULL){} 
  7. }; 
  8. typedef struct TreeNode TreeNode; 

访问函数  

 
 
  1. void visit(TreeNode *root) 
  2.         if(root) 
  3.                 cout <<root->val<<" "

    前序递归


  1. void preorder(TreeNode *root)
  2.         if(root) 
  3.         { 
  4.                 visit(root); 
  5.                 preorder(root->left); 
  6.                 preorder(root->right); 
  7.         } 
前序非递归 
 
  1. void preorder1(TreeNode *root) 
  2.         stack<TreeNode*> s; 
  3.         while(root || !s.empty()) 
  4.         { 
  5.                 if(root) 
  6.                 { 
  7.                         visit(root); 
  8.                         s.push(root->right); 
  9.                         root = root->left; 
  10.                 } 
  11.                 else 
  12.                 { 
  13.                         root = s.top(); 
  14.                         s.pop(); 
  15.                 } 
  16.         } 

  中序递归  

 
 
  1. void inorder(TreeNode *root) 
  2.         if(root) 
  3.         { 
  4.                 inorder(root->left); 
  5.                 visit(root); 
  6.                 inorder(root->right); 
  7.         } 

中序非递归  

 
 
  1. void inorder1(TreeNode *root) 
  2.         stack<TreeNode*> s; 
  3.         while(root || !s.empty()) 
  4.         { 
  5.                 if(root) 
  6.                 { 
  7.                         s.push(root); 
  8.                         root = root->left; 
  9.                 } 
  10.                 else 
  11.                 { 
  12.                         root = s.top(); 
  13.                         s.pop(); 
  14.                         visit(root); 
  15.                         root = root->right; 
  16.                 } 
  17.         } 

    后序递归  

 
 
  1. void postorder(TreeNode *root) 
  2.         if(root) 
  3.         { 
  4.                 postorder(root->left); 
  5.                 postorder(root->right); 
  6.                 visit(root); 
  7.         } 

后序非递归1,使用双栈  

 
 
  1. void postorder1(TreeNode *root) 
  2.         stack<TreeNode*> s1; 
  3.         stack<TreeNode*> s2; 
  4.         if(!root) return
  5.         s1.push(root); 
  6.         while(!s1.empty()) 
  7.         { 
  8.                 s2.push(s1.top()); 
  9.                 s1.pop(); 
  10.                 if(s2.top()->left) 
  11.                         s1.push(s2.top()->left); 
  12.                 if(s2.top()->right) 
  13.                         s1.push(s2.top()->right); 
  14.         } 
  15.         while(!s2.empty()) 
  16.         { 
  17.                 visit(s2.top()); 
  18.                 s2.pop(); 
  19.         } 

后序非递归2,使用一个pre指针  

 
 
  1. void postorder2(TreeNode *root) 
  2.         stack<TreeNode*> s; 
  3.         TreeNode *pre = NULL; 
  4.         TreeNode *cur; 
  5.         if(!root) return
  6.         s.push(root); 
  7.         while(!s.empty()) 
  8.         { 
  9.                 cur = s.top(); 
  10.                 if(!pre || (pre != cur->right && pre != cur->left)) 
  11.                 { 
  12.                         if(!cur->left && !cur->right) 
  13.                         { 
  14.                                 visit(cur); 
  15.                                 s.pop(); 
  16.                                 pre = cur; 
  17.                         } 
  18.                         if(cur->right) 
  19.                                 s.push(cur->right); 
  20.                         if(cur->left) 
  21.                                 s.push(cur->left); 
  22.                 } 
  23.  
  24.                 else if( pre == cur->left) 
  25.                 { 
  26.                         if(cur->right) 
  27.                                 s.push(cur->right); 
  28.                         else 
  29.                         { 
  30.                                 visit(cur); 
  31.                                 s.pop(); 
  32.                                 pre = cur; 
  33.                         } 
  34.                 } 
  35.                 else if(pre == cur->right) 
  36.                 { 
  37.                         visit(cur); 
  38.                         s.pop(); 
  39.                         pre = cur; 
  40.                 } 
  41.         } 

层次遍历,使用队列  

 
 
  1. void level_traversal(TreeNode *root) 
  2.         queue<TreeNode*> q; 
  3.         if(!root) return
  4.         q.push(root); 
  5.         while(!q.empty()) 
  6.         { 
  7.                 if(q.front()->left) 
  8.                         q.push(q.front()->left); 
  9.                 if(q.front()->right) 
  10.                         q.push(q.front()->right); 
  11.                 visit(q.front()); 
  12.                 q.pop(); 
  13.         } 

(更新中)

点赞