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

LintCode: Binary Tree Preorder Traversal

时间:2015-11-27 00:39:53      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

C++,递归

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> result;
23         if (root == NULL) {
24             return result;
25         }
26         result.insert(result.end(), root->val);
27         if (root->left != NULL) {
28             vector<int> left = preorderTraversal(root->left);
29             result.reserve(result.size()+left.size());
30             result.insert(result.end(), left.begin(), left.end());
31         }
32         if (root->right != NULL) {
33             vector<int> right = preorderTraversal(root->right);
34             result.reserve(result.size()+right.size());
35             result.insert(result.end(), right.begin(), right.end());
36         }
37         return result;
38     }
39 };

C++,递归,辅助函数

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> result;
23         if (root == NULL) {
24             return result;
25         } else {
26             preorderCore(root, result);
27         }
28         return result;
29     }
30     void preorderCore(TreeNode *root, vector<int> &result) {
31         if (root == NULL) {
32             return ;
33         }
34         result.insert(result.end(), root->val);
35         if (root->left != NULL) {
36             preorderCore(root->left, result);
37         }
38         if (root->right != NULL) {
39             preorderCore(root->right, result);
40         }
41     }
42 };

 C++,非递归

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> result;
23         if (root == NULL) {
24             return result;
25         }
26         stack<TreeNode *> sta;
27         TreeNode *cur = root;
28         while (cur!=NULL || !sta.empty()) {
29             while (cur != NULL) {
30                 result.insert(result.end(), cur->val);
31                 sta.push(cur);
32                 cur = cur->left;
33             }
34             cur = sta.top();
35             sta.pop();
36             cur = cur->right;
37         }
38         return result;
39     }
40 };

 

LintCode: Binary Tree Preorder Traversal

标签:

原文地址:http://www.cnblogs.com/CheeseZH/p/4999265.html

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