From 679b788dde386171b2cedb5d66516553ac484c23 Mon Sep 17 00:00:00 2001 From: Kuang Qin Date: Thu, 30 Mar 2017 01:03:21 -0500 Subject: [PATCH 1/2] Add Solution for 094 Binary Tree Inorder Traversal --- cpp/094_Binary_Tree_Inorder_Traversal.cpp | 131 ++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 cpp/094_Binary_Tree_Inorder_Traversal.cpp diff --git a/cpp/094_Binary_Tree_Inorder_Traversal.cpp b/cpp/094_Binary_Tree_Inorder_Traversal.cpp new file mode 100644 index 0000000..59da8cf --- /dev/null +++ b/cpp/094_Binary_Tree_Inorder_Traversal.cpp @@ -0,0 +1,131 @@ +// 94. Binary Tree Inorder Traversal +/** + * Given a binary tree, return the inorder traversal of its nodes' values. + * + * For example: + * Given binary tree [1,null,2,3], + * 1 + * \ + * 2 + * / + * 3 + * return [1,3,2]. + * + * Note: Recursive solution is trivial, could you do it iteratively? + * + * Tags: Tree, Hash Table, Stack + * + * Similar Problems: (M) Validate Binary Search Tree, (M) Binary Tree Preorder Traversal, (H) Binary Tree Postorder Traversal, + * (M) Binary Search Tree Iterator (M) Kth Smallest Element in a BST (H) Closest Binary Search Tree Value II (M) Inorder Successor in BST + * + * Author: Kuang Qin + */ + +#include +#include +#include + +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) {} + TreeNode(int x, TreeNode* l, TreeNode* r) : val(x), left(l), right(r) {} +}; + +// recursive solution +// time: O(n), space: O(n) - function call stack +class Solution { + void inorder(TreeNode* root, vector& nodes) { + if (root == NULL) { + return; + } + + inorder(root->left, nodes); + nodes.push_back(root->val); + inorder(root->right, nodes); + return; + } +public: + vector inorderTraversal(TreeNode* root) { + vector res; + inorder(root, res); + return res; + } +}; + +// iterative solution using stack +// time: O(n), space: O(n) - stack +class Solution_Iter { +public: + vector inorderTraversal(TreeNode* root) { + vector res; + stack st; + TreeNode *curr = root; + while (curr != NULL || !st.empty()) { + if (curr != NULL) { + st.push(curr); + curr = curr->left; + } + else { + curr = st.top(); + st.pop(); + res.push_back(curr->val); + curr = curr->right; + } + } + + return res; + } +}; + +// Morris traversal +// time: O(n) - find prev pointer, space: O(1) +class Solution_Morris { +public: + vector inorderTraversal(TreeNode* root) { + vector res; + TreeNode *curr = root, *prev = NULL; + while (curr != NULL) { + if (curr->left == NULL) { + res.push_back(curr->val); + curr = curr->right; + } + else { + prev = curr->left; + while (prev->right != NULL && prev->right != curr) { + prev = prev->right; + } + + if (prev->right == NULL) { + prev->right = curr; // change tree shape + curr = curr->left; + } + else { // prev->right == curr + prev->right == NULL; // recover tree shape + res.push_back(curr->val); + curr = curr->right; + } + } + } + + return res; + } +}; + +int main() { + TreeNode node3(3), node2(2, &node3, NULL), node1(1, NULL, &node2); + Solution sol; + vector res = sol.inorderTraversal(&node1); + for (int i = 0; i < res.size(); i++) { + cout << res[i] << " "; + } + cout << endl; + cin.get(); + return 0; +} \ No newline at end of file From 33a0c39931e824dec0796988d4c691f88c498dee Mon Sep 17 00:00:00 2001 From: kuangami Date: Thu, 30 Mar 2017 01:40:58 -0500 Subject: [PATCH 2/2] Fix evaluation error --- cpp/094_Binary_Tree_Inorder_Traversal.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/094_Binary_Tree_Inorder_Traversal.cpp b/cpp/094_Binary_Tree_Inorder_Traversal.cpp index 59da8cf..34e1a3f 100644 --- a/cpp/094_Binary_Tree_Inorder_Traversal.cpp +++ b/cpp/094_Binary_Tree_Inorder_Traversal.cpp @@ -107,7 +107,7 @@ class Solution_Morris { curr = curr->left; } else { // prev->right == curr - prev->right == NULL; // recover tree shape + prev->right = NULL; // recover tree shape res.push_back(curr->val); curr = curr->right; } @@ -128,4 +128,4 @@ int main() { cout << endl; cin.get(); return 0; -} \ No newline at end of file +}