Leetcode94.二叉数的中序遍历练习

初始代码

问题:未完成

cs 复制代码
/**
 * 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().
 */
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int* n=(int*)malloc(sizeof(int)*(returnSize));
    int m=0;
    struct TreeNode* new=root;
    struct TreeNode* current=new;
    while(m<returnSize)
    {  new=root;
        while(new)
       {  while(new->left)
         {
            new=new->left;
            current=new;
         }
        n[m++]=new->val;
        if(new->right==NULL)
          new=NULL;
        else new=new->right;
       }
       new=current;
    }
}

反思问题:

1.尝试过迭代与递归两种方法(对于迭代,知道要找到每个节点的父节点,但没想到用栈来存储,用下标的改变来回到父节点的位置;对于递归,知道"左 根 右"的顺序,但仅在意找到节点位置,没有想到可以直接在递归函数中就记录下每个节点的数据),对中序遍历的底层逻辑不熟练,无法把自己的逻辑思路转换成算法思维。

2.returnSize为指针,理解错意义

最终AC代码

1.迭代

cs 复制代码
/**
 * 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().
 */
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int top=-1,count=0;
    struct TreeNode** stack=(struct TreeNode**)malloc(sizeof(struct TreeNode*)*100);
    struct TreeNode* current=root;
    while(top!=-1||current!=NULL)
    {
        while(current!=NULL)
        {
            stack[++top]=current;
            current=current->left;
        }
        current=stack[top--];
        count++;

        current=current->right;
    }
    *returnSize=count;
    int* n=(int*)malloc(sizeof(int)*(count));
    top=-1;current=root;
    int m=0;
      while(top!=-1||current!=NULL)
    {
        while(current!=NULL)
        {
            stack[++top]=current;
            current=current->left;
        }
        current=stack[top--];
        n[m++]=current->val;

        current=current->right;
    }
    return n;
}

2.递归

cs 复制代码
/**
 * 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().
 */
void me(struct TreeNode* root,int* n,int* count)
{
    if(root==NULL)
      return;
    me(root->left,n,count);
    n[(*count)++]=root->val;
    me(root->right,n,count);  
}
void sum(struct TreeNode* root,int *returnSize)
{
   if(root==NULL)  return;
   sum(root->left,returnSize);
    (*returnSize)++;//注意指针++与指针指向数据++的区别
   sum(root->right,returnSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
   struct TreeNode* current=root;
   *returnSize=0;
   sum(current,returnSize);
   int num=*returnSize;
   int* n=(int*)malloc(sizeof(int)*num);
   int count=0;
   me(current,n,&count);//注意第三个形参类型
    return n;
}
相关推荐
YY_TJJ19 分钟前
算法题——贪心算法
算法·贪心算法
C++ 老炮儿的技术栈26 分钟前
include″″与includ<>的区别
c语言·开发语言·c++·算法·visual studio
新子y30 分钟前
【小白笔记】岛屿数量
笔记·python
Larry_Yanan36 分钟前
QML学习笔记(四十三)QML与C++交互:上下文属性暴露
c++·笔记·qt·学习·ui·交互
RainbowC01 小时前
GapBuffer高效标记管理算法
android·算法
liu****1 小时前
10.queue的模拟实现
开发语言·数据结构·c++·算法
mit6.8241 小时前
10.17 枚举中间|图论
算法
shinelord明1 小时前
【大数据技术实战】Kafka 认证机制全解析
大数据·数据结构·分布式·架构·kafka
新子y1 小时前
【小白笔记】岛屿的周长(Island Perimeter)
笔记·python
霜绛1 小时前
Unity:UGUI笔记(一)——三大基础控件、组合控件
笔记·学习·unity·游戏引擎