Binary Tree Preorder Traversal Solutions in C++
Number 144
Difficulty Medium
Acceptance 55.8%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/binary-tree-preorder-traversal/// Author : Hao Chen// Date : 2014-07-21#include <stdio.h>#include <stdlib.h>#include <time.h>#include <iostream>#include <vector>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};vector<int> preorderTraversal1(TreeNode *root);vector<int> preorderTraversal2(TreeNode *root);vector<int> preorderTraversal(TreeNode *root) {if (random()%2){cout << "---method one---" << endl;return preorderTraversal1(root);}cout << "---method two---" << endl;return preorderTraversal2(root);}vector<int> preorderTraversal1(TreeNode *root) {vector<int> v;vector<TreeNode*> stack;if (root) {stack.push_back(root);}while (stack.size()>0){TreeNode* n = stack.back();v.push_back(n->val);stack.pop_back();if (n->right){stack.push_back(n->right);}if (n->left){stack.push_back(n->left);}}return v;}vector<int> preorderTraversal2(TreeNode *root) {vector<int> v;vector<TreeNode*> stack;stack.push_back((TreeNode*)NULL);TreeNode *top = root;while(top!=NULL){v.push_back(top->val);if (top->right !=NULL){stack.push_back(top->right);}if (top->left != NULL){stack.push_back(top->left);}top = stack.back();stack.pop_back();}return v;}TreeNode* createTree(int a[], int n){if (n<=0) return NULL;TreeNode **tree = new TreeNode*[n];for(int i=0; i<n; i++) {if (a[i]==0 ){tree[i] = NULL;continue;}tree[i] = new TreeNode(a[i]);}int pos=1;for(int i=0; i<n && pos<n; i++) {if (tree[i]){tree[i]->left = tree[pos++];if (pos<n){tree[i]->right = tree[pos++];}}}return tree[0];}void printTree_pre_order(TreeNode *root){if (root == NULL){//cout << "# ";return ;}cout << root->val << " ";printTree_pre_order(root->left);printTree_pre_order(root->right);}void printArray(vector<int> v){for(int i=0; i<v.size(); i++){cout << v[i] << " ";}cout << endl;}int main(){srand(time(0));int a[] = {1,2,3,4,5,0,6,0,0,7,8,9,0};TreeNode* p = createTree(a, sizeof(a)/sizeof(int));printTree_pre_order(p);cout << endl;vector<int> v = preorderTraversal(p);printArray(v);cout << endl;return 0;}
C++ solution by pezy/LeetCode
#include <stack>#include <vector>using std::vector; using std::stack;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:vector<int> preorderTraversal(TreeNode *root) {vector<int> ret;for (stack<TreeNode *> s; root || !s.empty(); )if (root){s.push(root->right);ret.push_back(root->val);root = root->left;}else{root = s.top(); s.pop();}return ret;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Recursive// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;preorderTraversal(root, res);return res;}private:void preorderTraversal(TreeNode* node, vector<int> &res){if(node){res.push_back(node->val);preorderTraversal(node->left, res);preorderTraversal(node->right, res);}}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <vector>#include <stack>#include <cassert>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// My Non-Recursive// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {private:struct Command{string s; // go, printTreeNode* node;Command(string s, TreeNode* node): s(s), node(node){}};public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<Command> stack;stack.push(Command("go", root));while(!stack.empty()){Command command = stack.top();stack.pop();if(command.s == "print")res.push_back(command.node->val);else{assert(command.s == "go");if(command.node->right)stack.push(Command("go",command.node->right));if(command.node->left)stack.push(Command("go",command.node->left));stack.push(Command("print", command.node));}}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <vector>#include <stack>#include <cassert>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Classic Non-Recursive algorithm for preorder traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack;stack.push(root);while(!stack.empty()){TreeNode* curNode = stack.top();stack.pop();res.push_back(curNode->val);if(curNode->right)stack.push(curNode->right);if(curNode->left)stack.push(curNode->left);}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30#include <iostream>#include <vector>#include <stack>#include <cassert>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Another Classic Non-Recursive algorithm for preorder traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack;TreeNode* cur = root;while(cur != NULL || !stack.empty()){while(cur != NULL){res.push_back(cur->val);stack.push(cur);cur = cur->left;}cur = stack.top();stack.pop();cur = cur->right;}return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {TreeNode* root = new TreeNode(1);root->right = new TreeNode(2);root->right->left = new TreeNode(3);vector<int> res = Solution().preorderTraversal(root);print_vec(res);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30#include <iostream>#include <vector>#include <stack>#include <cassert>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Another Classic Non-Recursive algorithm for preorder traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack;TreeNode* cur = root;while(cur != NULL || !stack.empty()){if(cur != NULL){res.push_back(cur->val);stack.push(cur);cur = cur->left;}else{cur = stack.top();stack.pop();cur = cur->right;}}return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {TreeNode* root = new TreeNode(1);root->right = new TreeNode(2);root->right->left = new TreeNode(3);vector<int> res = Solution().preorderTraversal(root);print_vec(res);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-29#include <iostream>#include <vector>#include <stack>#include <cassert>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// PreOrder Morris Traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(1)class Solution {public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;TreeNode* cur = root;while(cur != NULL){if(cur->left == NULL){res.push_back(cur->val);cur = cur->right;}else{TreeNode* prev = cur->left;while(prev->right != NULL && prev->right != cur)prev = prev->right;if(prev->right == NULL){res.push_back(cur->val);prev->right = cur;cur = cur->left;}else{prev->right = NULL;cur = cur->right;}}}return res;}};int main() {return 0;}