【树】前序,中序,后序的非递归遍历

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

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.     }



























<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(4) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值