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;
}
相关推荐
com_4sapi3 分钟前
2025 权威认证头部矩阵系统全景对比发布 双榜单交叉验证
大数据·c语言·人工智能·算法·矩阵·机器人
前端小L5 分钟前
二分查找专题(九):“降维”的魔术!将二维矩阵“拉平”为一维
数据结构·算法
Jasmine_llq26 分钟前
《P7516 [省选联考 2021 A/B 卷] 图函数》
算法·弗洛伊德算法·floydwarshall算法·后缀和计算
kaikaile199526 分钟前
三维CT图像重建算法
算法
她说人狗殊途28 分钟前
时间复杂度(按增长速度从低到高排序)包括以下几类,用于描述算法执行时间随输入规模 n 增长的变化趋势:
数据结构·算法·排序算法
计科土狗29 分钟前
算法基础入门第一章
c++·算法
与己斗其乐无穷29 分钟前
算法(二)滑动窗口
算法
Ghost-Face29 分钟前
恭喜自己,挑战成功!
算法
Miraitowa_cheems30 分钟前
LeetCode算法日记 - Day 102: 不相交的线
数据结构·算法·leetcode·深度优先·动态规划
野生技术架构师30 分钟前
盘一盘Redis的底层数据结构
数据结构·数据库·redis