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,如需转载请自行联系原作者

相关文章
|
11月前
Leetcode Binary Tree Postorder Traversal(面试题推荐)
非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。 只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。
47 0
LeetCode 145. Binary Tree Postorder Traversal
给定一个二叉树,返回它的后序遍历。
65 0
LeetCode 145. Binary Tree Postorder Traversal
|
Java
[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/难度:Medium题目:105.
970 0
[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/难度:Medium题目:106.
1130 0
[LeetCode] Binary Tree Preorder Traversal
This is a fundamental and yet classic problem. I share my three solutions here: Iterative solution using stack --- O(n) time and O(n) space; Recurs...
655 0
[LeetCode] Binary Tree Inorder Traversal
This is a fundamental and yet classic problem. I share my three solutions here: Iterative solution using stack --- O(n) time and O(n) space; Recurs...
700 0