Increasing Order Search Tree Solutions in C++
Number 897
Difficulty Easy
Acceptance 71.0%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/increasing-order-search-tree/description//// Author : liuyubobobo/// Time : 2018-09-01#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) {}};/// Inorder traversal and store all the nodes/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:TreeNode* increasingBST(TreeNode* root) {if(root == NULL)return NULL;vector<TreeNode*> nodes;inOrder(root, nodes);for(int i = 1; i < nodes.size(); i ++){nodes[i - 1]->left = NULL;nodes[i - 1]->right = nodes[i];}nodes.back()->left = nodes.back()->right = NULL;return nodes[0];}private:void inOrder(TreeNode* node, vector<TreeNode*>& nodes){if(node == NULL)return;inOrder(node->left, nodes);nodes.push_back(node);inOrder(node->right, nodes);}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/increasing-order-search-tree/description//// Author : liuyubobobo/// Time : 2018-09-01#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) {}};/// Inorder traversal and link during the traversal/// Time Complexity: O(n)/// Space Complexity: O(h) where h is the height of the treeclass Solution {public:TreeNode* increasingBST(TreeNode* root) {if(root == NULL)return NULL;TreeNode* tail;return inOrder(root, tail);}private:TreeNode* inOrder(TreeNode* node, TreeNode* &tail){if(node->left == NULL && node->right == NULL){tail = node;return node;}TreeNode* ret;if(node->left){ret = inOrder(node->left, tail);tail->left = NULL;tail->right = node;}elseret = node;node->left = NULL;tail = node;if(node->right) {TreeNode *root = inOrder(node->right, tail);node->right = root;}return ret;}};int main() {TreeNode* root = new TreeNode(1);root->left = new TreeNode(2);Solution().increasingBST(root);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/increasing-order-search-tree/description//// Author : liuyubobobo/// Time : 2018-09-01#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) {}};/// Inorder traversal and link during the traversal/// Using a class member TreeNode cur:)////// Time Complexity: O(n)/// Space Complexity: O(h) where h is the height of the treeclass Solution {private:TreeNode* cur;public:TreeNode* increasingBST(TreeNode* root) {if(root == NULL)return NULL;TreeNode* dummyRoot = new TreeNode(-1);cur = dummyRoot;inOrder(root);TreeNode* ret = dummyRoot->right;delete dummyRoot;return ret;}private:void inOrder(TreeNode* node){if(node == NULL)return;inOrder(node->left);cur->right = node;cur = cur->right;cur->left = NULL;inOrder(node->right);}};int main() {TreeNode* root = new TreeNode(1);root->left = new TreeNode(2);Solution().increasingBST(root);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/increasing-order-search-tree/description//// Author : liuyubobobo/// Time : 2018-09-02#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) {}};/// Using Morris Treversal/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {private:TreeNode* cur;public:TreeNode* increasingBST(TreeNode* root) {if(root == NULL)return NULL;TreeNode* dummyRoot = new TreeNode(-1);cur = dummyRoot;// Morris TraversalTreeNode* node = root;while(node){if(node->left == NULL){cur->right = node;cur = cur->right;cur->left = NULL;node = node->right;}else{TreeNode* prev = node->left;while(prev->right && prev->right != node)prev = prev->right;if(prev->right == NULL){prev->right = node;node = node->left;}else{prev->right = NULL;cur->right = node;cur = cur->right;cur->left = NULL;node = node->right;}}}TreeNode* ret = dummyRoot->right;delete dummyRoot;return ret;}};int main() {TreeNode* root = new TreeNode(1);root->left = new TreeNode(2);Solution().increasingBST(root);return 0;}