O(1) space using Morris Inorder Traversal
IN GFG WORKING WITH O(1) SPACE \n\n\nclass Solution {\n  public:\n  \n    int inorderTraversal(Node* root, int k) {\n\n        Node* current = root;\n        in
# IN GFG WORKING WITH O(1) SPACE \n\n```\nclass Solution {\n  public:\n  \n    int inorderTraversal(Node* root, int k) {\n\n        Node* current = root;\n        int counter = 0;\n        \n        while(current != nullptr){\n\n\n            if(current->left == nullptr){\n                counter++;\n                if (counter == k) {\n                    return current->data;\n                    break;\n                }\n                current = current->right;\n            }\n\n            else{\n                Node* left_root = current->left;\n\n                while(left_root->right != current && left_root->right != nullptr) {\n                    left_root = left_root->right;\n                }\n\n                if(left_root->right == nullptr){\n                    left_root->right = current;\n                    current = current->left;\n                }\n\n                else{\n                    left_root->right = nullptr;\n                    counter++;\n                    if (counter == k) {\n                       return current->data;\n                       break;\n                    }\n                    current = current->right;\n                    \n\n                }\n\n            }\n\n        }\n\n        return -1;\n    }\n\n    int KthSmallestElement(Node *root, int k) {\n        \n        return inorderTraversal(root, k);\n    }\n};\n```\n\n\n# BUT IN LEETCODE THEY DON\'T ALLOWING TO CHANGE TREE \n\n# THEREFORE I MADE A NEW TREE USING O(N) SPACE BUT IF changing tree is allowed then use above code, Otherwise use Recursive or Iterative inorder for solving.\n\n\n# Code\n```\nclass Solution {\npublic:\n\nTreeNode* copyBST(TreeNode* root) {\n    if (root == nullptr) {\n        return nullptr;\n    }\n\n    // Create a new node with the same value\n    TreeNode* newNode = new TreeNode(root->val);\n    \n    // Recursively copy the left and right subtrees\n    newNode->left = copyBST(root->left);\n    newNode->right = copyBST(root->right);\n    \n    return newNode;\n}\n\n int inorderTraversal(TreeNode* root, int k) {\n\n       TreeNode* new_root = copyBST(root);\n\n        TreeNode* current = new_root;\n        int counter = 0;\n        \n        while(current != nullptr){\n\n\n            if(current->left == nullptr){\n                counter++;\n                if (counter == k) {\n                    return current->val;\n                    break;\n                }\n                current = current->right;\n            }\n\n            else{\n                TreeNode* left_root = current->left;\n\n                while(left_root->right != current && left_root->right != nullptr) {\n                    left_root = left_root->right;\n                }\n\n                if(left_root->right == nullptr){\n                    left_root->right = current;\n                    current = current->left;\n                }\n\n                else{\n                    left_root->right = nullptr;\n                    counter++;\n                    if (counter == k) {\n                       return current->val;\n                       break;\n                    }\n                    current = current->right;\n                    \n\n                }\n\n            }\n\n        }\n\n        return -1;\n    }\n\n    int kthSmallest(TreeNode* root, int k) {\n\n        return inorderTraversal(root, k);\n        \n    }\n};\n```
['Tree', 'Depth-First Search', 'Binary Tree', 'C++']