LintCode: Binary Tree Preorder Traversal

简介:

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 };
复制代码

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4999265.html,如需转载请自行联系原作者

相关文章
|
机器学习/深度学习 算法 前端开发
将一维数组转变成二维数组
将一维数组转变成二维数组
115 0
Java开发——32.I/O流_处理流(RandomAccessFile流)
操作文件内容,结合前面所学的File类,我们可以读取和写出文件,也可以修改文件的字符集编码...
Java开发——32.I/O流_处理流(RandomAccessFile流)
|
存储 PHP
PHP的静态变量表是干什么的?底层原理是什么?
PHP的静态变量表是干什么的?底层原理是什么?
173 0
Yii2中的入口文件环境配置
默认的Debug配置  在入口文件中 defined ( 'YII_DEBUG' ) or define ( 'YII_DEBUG', true ); defined ( 'YII_ENV' ) or define ( 'YII_ENV', 'dev' ); 以上配置后,所有的异常会...
1038 0
|
5天前
|
人工智能 运维 安全
|
3天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
10天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
846 109