94. Binary Tree Inorder Traversal
Medium
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Return an array of size *returnSize.
- * Note: The returned array must be malloced, assume caller calls free().
- */
- typedef struct stk{
- int top;
- int *nd;
- }STK;
- void stk_init(STK *s)
- {
- s->top=0;
- }
- void stk_push(STK *s,int val)
- {
- if(s->top==0)
- s->nd = (int*)malloc(sizeof(int));
- else
- s->nd = (int*)realloc(s->nd,sizeof(int)*(s->top+1));
- s->nd[s->top]=val;
- s->top+=1;
- }
- void inorder(struct TreeNode* root, STK* stack){
- if(root==NULL)
- return;
- inorder(root->left,stack);
- stk_push(stack,root->val);
- inorder(root->right,stack);
- }
- int* inorderTraversal(struct TreeNode* root, int* returnSize) {
- STK* stack=(STK*)malloc(sizeof(STK));
- stk_init(stack);
- inorder(root,stack);
- *returnSize=stack->top;
- return stack->nd;
- }
迭代算法1
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Return an array of size *returnSize.
- * Note: The returned array must be malloced, assume caller calls free().
- */
- typedef struct stk{
- int top;
- struct TreeNode *nd;
- }STK;
- void stk_init(STK* s){
- s->top=0;
- s->nd=NULL;
- }
- void stk_push(STK* s, struct TreeNode* nd){
- if(nd==NULL)
- return;
- if(s->nd==NULL)
- s->nd = (struct TreeNode*)malloc(sizeof(struct TreeNode));
- else
- s->nd = (struct TreeNode*)realloc(s->nd, sizeof(struct TreeNode)*(s->top+1));
- s->nd[s->top].val = nd->val;
- s->nd[s->top].left = nd->left;
- s->nd[s->top].right = nd->right;
- s->top+=1;
- }
- struct TreeNode* stk_pop(STK* s){
- if(s->top<1)
- return NULL;
- struct TreeNode* r;
- s->top-=1;
- r = &(s->nd[s->top]);
- return r;
- }
- int stk_empty(STK* s){
- return s->top;
- }
- int* inorderTraversal(struct TreeNode* root, int* returnSize) {
- int* result=NULL;
- int index=0;
- STK* s = (STK*)malloc(sizeof(STK));
- if(root==NULL)
- return NULL;
- stk_init(s);
- while(stk_empty(s)!=0 || root!=NULL){
- while(root!=NULL){
- stk_push(s,root);
- root=root->left;
- }
- if(stk_empty(s)!=0){
- root=stk_pop(s);
- if(root!=NULL){
- if(result==NULL){
- result = (int*)malloc(sizeof(int));
- result[index] = root->val;
- }else{
- result = (int*)realloc(result, sizeof(int)*(index+1));
- result[index] = root->val;
- }
- index+=1;
- root=root->right;
- }
- }
- }
- *returnSize=index;
- return result;
- }
144. Binary Tree Preorder Traversal
Medium
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3]
Follow up: Recursive solution is trivial, could you do it iteratively?
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Return an array of size *returnSize.
- * Note: The returned array must be malloced, assume caller calls free().
- */
- typedef struct stk{
- int top;
- int *nd;
- }STK;
- void stk_init(STK* s){
- s->top=0;
- }
- void stk_push(STK* s, int val){
- if(s->top==0)
- s->nd = (int*)malloc(sizeof(int));
- else
- s->nd = (int*)realloc(s->nd, sizeof(int)*(s->top+1));
- s->nd[s->top] = val;
- s->top+=1;
- }
- void preorder(struct TreeNode* r, STK* s){
- if(r==NULL)
- return;
- stk_push(s, r->val);
- preorder(r->left, s);
- preorder(r->right, s);
- }
- int* preorderTraversal(struct TreeNode* root, int* returnSize) {
- STK* s = (STK*)malloc(sizeof(STK));
- stk_init(s);
- preorder(root, s);
- *returnSize = s->top;
- return s->nd;
- }
迭代代码开始
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Return an array of size *returnSize.
- * Note: The returned array must be malloced, assume caller calls free().
- */
- typedef struct stk{
- int top;
- struct TreeNode *nd;
- }STK;
- void stk_init(STK* s){
- s->top=0;
- s->nd=NULL;
- }
- void stk_push(STK* s, struct TreeNode* nd){
- if(nd==NULL)
- return;
- if(s->nd==NULL)
- s->nd = (struct TreeNode*)malloc(sizeof(struct TreeNode));
- else
- s->nd = (struct TreeNode*)realloc(s->nd, sizeof(struct TreeNode)*(s->top+1));
- s->nd[s->top].val = nd->val;
- s->nd[s->top].left = nd->left;
- s->nd[s->top].right = nd->right;
- s->top+=1;
- }
- struct TreeNode* stk_pop(STK* s){
- if(s->top<1)
- return NULL;
- struct TreeNode* r;
- s->top-=1;
- r = &(s->nd[s->top]);
- return r;
- }
- int stk_empty(STK* s){
- return s->top;
- }
- int* preorderTraversal(struct TreeNode* root, int* returnSize) {
- struct TreeNode* t;
- struct TreeNode* left;
- struct TreeNode* right;
- int* result=NULL;
- int index=0;
- STK* s = (STK*)malloc(sizeof(STK));
- if(root==NULL)
- return NULL;
- stk_init(s);
- stk_push(s, root);
- while(stk_empty(s)!=0){
- t = stk_pop(s);
- if(t!=NULL){
- if(result==NULL){
- result = (int*)malloc(sizeof(int));
- result[index] = t->val;
- }else{
- result = (int*)realloc(result, sizeof(int)*(index+1));
- result[index] = t->val;
- }
- index+=1;
- left=t->left;//下面push的时候,s->top会变,这里必须临时变量保存
- right=t->right;
- stk_push(s,right);
- stk_push(s,left);
- }
- }
- *returnSize=index;
- return result;
- }