Binary Tree Inorder Traversal Solutions in C++
Number 94
Difficulty Medium
Acceptance 63.5%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/binary-tree-inorder-traversal/// Author : Hao Chen// Date : 2014-06-27/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:vector<int> inorderTraversal(TreeNode *root) {vector<TreeNode*> stack;vector<int> v;while(stack.size()>0 || root!=NULL){if (root!=NULL){stack.push_back(root);root = root->left;}else{if (stack.size()>0) {root = stack.back();stack.pop_back();v.push_back(root->val);root = root->right;}}}return v;}};
C++ solution by pezy/LeetCode
#include <vector>#include <stack>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> inorderTraversal(TreeNode *root) {vector<int> ret;for (stack<TreeNode *> s; !s.empty() || root; ){if (root){s.push(root);root = root->left;}else{root = s.top(); s.pop();ret.push_back(root->val);root = root->right;}}return ret;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// 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> inorderTraversal(TreeNode* root) {vector<int> res;__inorderTraversal(root, res);return res;}private:void __inorderTraversal(TreeNode* node, vector<int> &res){if( node ){__inorderTraversal(node->left, res);res.push_back( node->val );__inorderTraversal(node->right, res);}}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <vector>#include <stack>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> inorderTraversal(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));stack.push(Command("print", command.node));if(command.node->left)stack.push(Command("go",command.node->left));}}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// Author : liuyubobobo/// Time : 2017-05-30#include <iostream>#include <vector>#include <stack>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 inorder 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> inorderTraversal(TreeNode* root) {vector<int> res;if( root == NULL )return res;stack<TreeNode*> stack;TreeNode* cur = root;while(cur != NULL || !stack.empty()){while(cur != NULL){stack.push(cur);cur = cur->left;}cur = stack.top();stack.pop();res.push_back(cur->val);cur = cur->right;}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// Author : liuyubobobo/// Time : 2017-05-30#include <iostream>#include <vector>#include <stack>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 inorder 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> inorderTraversal(TreeNode* root) {vector<int> res;if( root == NULL )return res;stack<TreeNode*> stack;TreeNode* cur = root;while(cur != NULL || !stack.empty()){if(cur != NULL){stack.push(cur);cur = cur->left;}else {cur = stack.top();stack.pop();res.push_back(cur->val);cur = cur->right;}}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// Author : liuyubobobo/// Time : 2018-05-30#include <iostream>#include <vector>#include <stack>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) {}};// InOrder Morris Traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(1)class Solution {public:vector<int> inorderTraversal(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){prev->right = cur;cur = cur->left;}else{prev->right = NULL;res.push_back(cur->val);cur = cur->right;}}}return res;}};int main() {return 0;}