Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121989
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-24 16:22
文章存档

2014年(53)

我的朋友

分类: C/C++

2014-09-01 21:58:36

树其实是递归定义的,所以递归遍历树的方式也是比较自然的。但是因为种种原因---比如面试官想测你智商---你必须采用非递归的方法遍历树,那么请记住以下两个诀窍:

1. 树的非递归遍历是对递归遍历的模仿,而怎么模仿递归呢?使用栈和循环

2. 都只需要一个循环

下面是code,都通过了leetcode的测试:

点击(此处)折叠或打开

  1. /*前序遍历*/
  2. vector<int> preorderTraversal(TreeNode *root){
  3.         vector<int> result;
  4.         if(root!=NULL){
  5.             stack<TreeNode *> s;
  6.             bool done=false;
  7.             TreeNode * current = root;
  8.             while(!done){
  9.                 if(current!=NULL){
  10.                     s.push(current);
  11.                     result.push_back(current->val);
  12.                     current=current->left;
  13.                 }
  14.                 else{
  15.                     if(s.empty()){
  16.                         done=true;
  17.                     }
  18.                     else{
  19.                         current=s.top();
  20.                         s.pop();
  21.                         current=current->right;
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.         return result;
  27.     }
  28. /*中序遍历*/
  29. vector<int> inorderTraversal(TreeNode *root){
  30.         vector<int> result;
  31.         if(root!=NULL){
  32.             stack<TreeNode *> s;
  33.             bool done=false;
  34.             TreeNode * current = root;
  35.             while(!done){
  36.                 if(current!=NULL){
  37.                     s.push(current);
  38.                    
  39.                     current=current->left;
  40.                 }
  41.                 else{
  42.                     if(s.empty()){
  43.                         done=true;
  44.                     }
  45.                     else{
  46.                         current=s.top();
  47.                         result.push_back(current->val);
  48.                         s.pop();
  49.                         current=current->right;
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         return result;
  55.     }
  56. /*后序遍历*/
  57. vector<int> postorderTraversal(TreeNode *root) {
  58.         stack<TreeNode *> s;
  59.         vector<int> result;
  60.         if(root!=NULL){
  61.             s.push(root);
  62.             while(!s.empty()){
  63.                 TreeNode * current=s.top();
  64.                 s.pop();
  65.                 result.push_back(current->val);
  66.                 if(current->left!=NULL)
  67.                     s.push(current->left);
  68.                 if(current->right!=NULL)
  69.                     s.push(current->right);
  70.             }
  71.         }
  72.         reverse(result.begin(), result.end());
  73.         return result;
  74.     }



























阅读(729) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~