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;
}
相关推荐
_F_y2 小时前
MySQL用C/C++连接
c语言·c++·mysql
BackCatK Chen2 小时前
C语言学习栏目目录
c语言·保姆级教程·c语言入门·c语言学习栏目目录
不会代码的小猴3 小时前
Linux环境编程第六天笔记--system-V IPC
linux·笔记
乌恩大侠3 小时前
【笔记】USRP 5G 和 6G 参考架构
笔记·5g
biuyyyxxx4 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化
极客数模4 小时前
【2026美赛赛题初步翻译F题】2026_ICM_Problem_F
大数据·c语言·python·数学建模·matlab
舟舟亢亢4 小时前
Java集合笔记总结
java·笔记
L_09074 小时前
【C++】高阶数据结构 -- 红黑树
数据结构·c++
A_nanda5 小时前
c# MOdbus rto读写串口,如何不相互影响
算法·c#·多线程
丝斯20115 小时前
AI学习笔记整理(66)——多模态大模型MOE-LLAVA
人工智能·笔记·学习