/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define MAX 101
void visit(struct TreeNode* root,int* returnSize,int* arr)
{
if(root)
{
visit(root->left,returnSize,arr);
arr[(*returnSize)++] = root->val;
visit(root->right,returnSize,arr);
}
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* arr = (int*)malloc(sizeof(int) * MAX);
(*returnSize) = 0;
visit(root,returnSize,arr);
return arr;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define MAX 101
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* arr = (int*)malloc(sizeof(int) * MAX);
(*returnSize) = 0;
struct TreeNode* position;
while(root)
{
position = root->left;
if(!position)
{
arr[(*returnSize)++] = root->val;
root = root->right;
}
else
{
while(position->right != root && position->right)
position = position->right;
if(!position->right)
{
position->right = root;
root = root->left;
}
else
{
position->right = NULL;
arr[(*returnSize)++] = root->val;
root = root->right;
}
}
}
return arr;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define MAX 101
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* arr = (int*)malloc(sizeof(int) * MAX),size = 0;
(*returnSize) = 0;
struct TreeNode* position = root,* stack[MAX];
while(position != NULL || size > 0)
{
while(position != NULL)
{
stack[size++] = position;
position = position->left;
}
position = stack[--size];
arr[(*returnSize)++] = position->val;
position = position->right;
}
return arr;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> ret;
void visit(TreeNode* root)
{
if(!root) return;
visit(root->left);
ret.push_back(root->val);
visit(root->right);
return;
}
vector<int> inorderTraversal(TreeNode* root) {
visit(root);
return ret;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* visit(const vector<int>& nums,int left,int right)
{
if(left > right) return nullptr;
int pos = (left+right)/2;
auto root = new TreeNode(nums[pos],visit(nums,left,pos-1),visit(nums,pos+1,right));
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return visit(nums,0,nums.size()-1);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
struct TreeNode* pos = root;
stack<TreeNode*> stack;
vector<int> ret;
while(stack.size() || pos)
{
while(pos)
{
stack.emplace(pos);
pos = pos->left;
}
if(stack.size())
{
ret.emplace_back(stack.top()->val);
pos = stack.top()->right;
stack.pop();
}
}
return ret;
}
};