Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1545068
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2019-01-01 23:07:23

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?


  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  * int val;
  5.  * struct TreeNode *left;
  6.  * struct TreeNode *right;
  7.  * };
  8.  */
  9. /**
  10.  * Return an array of size *returnSize.
  11.  * Note: The returned array must be malloced, assume caller calls free().
  12.  */
  13. typedef struct stk{
  14.    int top;
  15.    int *nd;
  16. }STK;

  17. void stk_init(STK *s)
  18. {
  19.     s->top=0;
  20. }

  21. void stk_push(STK *s,int val)
  22. {
  23.     if(s->top==0)
  24.         s->nd = (int*)malloc(sizeof(int));
  25.     else
  26.         s->nd = (int*)realloc(s->nd,sizeof(int)*(s->top+1));
  27.     s->nd[s->top]=val;
  28.     s->top+=1;
  29. }

  30. void inorder(struct TreeNode* root, STK* stack){
  31.     if(root==NULL)
  32.         return;
  33.     inorder(root->left,stack);
  34.     stk_push(stack,root->val);
  35.     inorder(root->right,stack);
  36. }
  37. int* inorderTraversal(struct TreeNode* root, int* returnSize) {
  38.     STK* stack=(STK*)malloc(sizeof(STK));
  39.     stk_init(stack);
  40.     inorder(root,stack);
  41.     *returnSize=stack->top;
  42.     return stack->nd;
  43. }

迭代算法1
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  * int val;
  5.  * struct TreeNode *left;
  6.  * struct TreeNode *right;
  7.  * };
  8.  */
  9. /**
  10.  * Return an array of size *returnSize.
  11.  * Note: The returned array must be malloced, assume caller calls free().
  12.  */
  13. typedef struct stk{
  14.     int top;
  15.     struct TreeNode *nd;
  16. }STK;
  17. void stk_init(STK* s){
  18.     s->top=0;
  19.     s->nd=NULL;
  20. }
  21. void stk_push(STK* s, struct TreeNode* nd){
  22.     if(nd==NULL)
  23.         return;
  24.     if(s->nd==NULL)
  25.         s->nd = (struct TreeNode*)malloc(sizeof(struct TreeNode));
  26.     else
  27.         s->nd = (struct TreeNode*)realloc(s->nd, sizeof(struct TreeNode)*(s->top+1));
  28.     s->nd[s->top].val = nd->val;
  29.     s->nd[s->top].left = nd->left;
  30.     s->nd[s->top].right = nd->right;
  31.     s->top+=1;
  32. }

  33. struct TreeNode* stk_pop(STK* s){
  34.     if(s->top<1)
  35.         return NULL;
  36.     struct TreeNode* r;
  37.     s->top-=1;
  38.     r = &(s->nd[s->top]);
  39.     return r;
  40. }

  41. int stk_empty(STK* s){
  42.     return s->top;
  43. }
  44. int* inorderTraversal(struct TreeNode* root, int* returnSize) {
  45.     int* result=NULL;
  46.     int index=0;
  47.     STK* s = (STK*)malloc(sizeof(STK));
  48.     if(root==NULL)
  49.        return NULL;

  50.     stk_init(s);

  51.     while(stk_empty(s)!=0 || root!=NULL){
  52.         while(root!=NULL){
  53.             stk_push(s,root);
  54.             root=root->left;
  55.         }
  56.         if(stk_empty(s)!=0){
  57.             root=stk_pop(s);
  58.             if(root!=NULL){
  59.                 if(result==NULL){
  60.                     result = (int*)malloc(sizeof(int));
  61.                     result[index] = root->val;
  62.                 }else{
  63.                     result = (int*)realloc(result, sizeof(int)*(index+1));
  64.                     result[index] = root->val;
  65.                 }
  66.                 index+=1;
  67.                 root=root->right;
  68.             }
  69.         }
  70.     }
  71.     *returnSize=index;
  72.     return result;
  73. }
迭代算法1



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?

递归代码开始
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  * int val;
  5.  * struct TreeNode *left;
  6.  * struct TreeNode *right;
  7.  * };
  8.  */
  9. /**
  10.  * Return an array of size *returnSize.
  11.  * Note: The returned array must be malloced, assume caller calls free().
  12.  */
  13. typedef struct stk{
  14.     int top;
  15.     int *nd;
  16. }STK;
  17. void stk_init(STK* s){
  18.     s->top=0;
  19. }
  20. void stk_push(STK* s, int val){
  21.     if(s->top==0)
  22.         s->nd = (int*)malloc(sizeof(int));
  23.     else
  24.         s->nd = (int*)realloc(s->nd, sizeof(int)*(s->top+1));
  25.     s->nd[s->top] = val;
  26.     s->top+=1;
  27. }
  28. void preorder(struct TreeNode* r, STK* s){
  29.     if(r==NULL)
  30.         return;
  31.     stk_push(s, r->val);
  32.     preorder(r->left, s);
  33.     preorder(r->right, s);
  34. }
  35. int* preorderTraversal(struct TreeNode* root, int* returnSize) {
  36.     STK* s = (STK*)malloc(sizeof(STK));
  37.     stk_init(s);
  38.     preorder(root, s);
  39.     *returnSize = s->top;
  40.     return s->nd;
  41. }
递归代码结束


迭代代码开始
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  * int val;
  5.  * struct TreeNode *left;
  6.  * struct TreeNode *right;
  7.  * };
  8.  */
  9. /**
  10.  * Return an array of size *returnSize.
  11.  * Note: The returned array must be malloced, assume caller calls free().
  12.  */
  13. typedef struct stk{
  14.     int top;
  15.     struct TreeNode *nd;
  16. }STK;
  17. void stk_init(STK* s){
  18.     s->top=0;
  19.     s->nd=NULL;
  20. }
  21. void stk_push(STK* s, struct TreeNode* nd){
  22.     if(nd==NULL)
  23.         return;
  24.     if(s->nd==NULL)
  25.         s->nd = (struct TreeNode*)malloc(sizeof(struct TreeNode));
  26.     else
  27.         s->nd = (struct TreeNode*)realloc(s->nd, sizeof(struct TreeNode)*(s->top+1));
  28.     s->nd[s->top].val = nd->val;
  29.     s->nd[s->top].left = nd->left;
  30.     s->nd[s->top].right = nd->right;
  31.     s->top+=1;
  32. }

  33. struct TreeNode* stk_pop(STK* s){
  34.     if(s->top<1)
  35.         return NULL;
  36.     struct TreeNode* r;
  37.     s->top-=1;
  38.     r = &(s->nd[s->top]);
  39.     return r;
  40. }

  41. int stk_empty(STK* s){
  42.     return s->top;
  43. }

  44. int* preorderTraversal(struct TreeNode* root, int* returnSize) {
  45.     struct TreeNode* t;
  46.     struct TreeNode* left;
  47.     struct TreeNode* right;
  48.     int* result=NULL;
  49.     int index=0;
  50.     STK* s = (STK*)malloc(sizeof(STK));
  51.     if(root==NULL)
  52.         return NULL;
  53.     stk_init(s);
  54.     stk_push(s, root);
  55.     while(stk_empty(s)!=0){
  56.         t = stk_pop(s);
  57.         if(t!=NULL){
  58.             if(result==NULL){
  59.                 result = (int*)malloc(sizeof(int));
  60.                 result[index] = t->val;
  61.             }else{
  62.                 result = (int*)realloc(result, sizeof(int)*(index+1));
  63.                 result[index] = t->val;
  64.             }
  65.             index+=1;
  66.             left=t->left;//下面push的时候,s->top会变,这里必须临时变量保存
  67.             right=t->right;
  68.             stk_push(s,right);
  69.             stk_push(s,left);
  70.         }
  71.     }
  72.     *returnSize=index;
  73.     return result;
  74. }
迭代代码结束














阅读(4510) | 评论(0) | 转发(0) |
0

上一篇:LeetCode 0100 0101

下一篇:LeetCode 108

给主人留下些什么吧!~~