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;
}
相关推荐
羊群智妍16 小时前
2026 AI搜索流量密码:免费GEO监测工具,优化效果看得见
笔记·百度·微信·facebook·新浪微博
那个村的李富贵17 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
power 雀儿17 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
Yvonne爱编码17 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
熬夜有啥好17 小时前
数据结构——哈希表
数据结构·散列表
琹箐17 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia118 小时前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱
坚持就完事了18 小时前
数据结构之树(Java实现)
java·算法
算法备案代理18 小时前
大模型备案与算法备案,企业该如何选择?
人工智能·算法·大模型·算法备案
山岚的运维笔记18 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver