码迷,mamicode.com
首页 > 其他好文 > 详细

第十六周 6.13 --- 6.19

时间:2016-06-19 14:12:07      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

6.19

我说15周怎么这么长...原来忘记新开一篇了...sigh 0-0

 

leetcode 144 Binary Tree Preorder Traversal

二叉树的前序遍历,非递归实现

技术分享
 1 class Solution{
 2 public:
 3     vector<int> preorderTraversal(TreeNode* root){
 4         vector<int> ans;
 5         stack<TreeNode*> s;
 6         if(root == NULL) return ans;
 7         else{
 8             s.push(root);
 9             while(!s.empty()){
10                 TreeNode* tmp = s.top();s.pop();
11                 ans.push_back(tmp->val);
12                 if(tmp->right != NULL) s.push(tmp->right);
13                 if(tmp->left != NULL) s.push(tmp->left);
14             }
15             return ans;
16         }
17     }
18 };
View Code

 

第十六周 6.13 --- 6.19

标签:

原文地址:http://www.cnblogs.com/wuyuewoniu/p/5597971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!