DAY 17
0

## 題目:

https://leetcode.com/problems/binary-tree-inorder-traversal/

## 解題思路:

### C版本:

``````void recursive(struct TreeNode* parent, int* recursiveResult, int* recursiveSize){
if(parent==NULL){
return;
}

if(parent->left==NULL && parent->right==NULL){
recursiveResult[*recursiveSize]=parent->val;
(*recursiveSize)++;
return;
}

if(parent->left!=NULL){
recursive(parent->left, recursiveResult, recursiveSize);
}

recursiveResult[*recursiveSize]=parent->val;
(*recursiveSize)++;

if(parent->right!=NULL){
recursive(parent->right, recursiveResult, recursiveSize);
}

return;
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* traversalResult=(int*)malloc(1000*sizeof(struct TreeNode));

recursive(root, traversalResult, returnSize);
return traversalResult;
}
``````

### Javascript版本:

``````var inorderTraversal = function(root) {
const stack = [];
const res = [];

while (root || stack.length) {
if (root) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
res.push(root.val);
root = root.right;
}
}
return res;
};
``````

Push() 將值放入陣列
Pop() 將值從陣列最後取出

## 程式Github分享:

https://github.com/SIAOYUCHEN/leetcode

## 相似主題分享:

Everyone who pursues a dream has a silent time, a day that only oneself knows, sometimes I spend a lot of effort and endure a lot of loneliness and loneliness.