当前位置: 首页 > news >正文

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

初始代码

问题:未完成

/*** 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.迭代

/*** 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.递归

/*** 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;
}

http://www.dtcms.com/a/403077.html

相关文章:

  • 多种解法全解析——力扣217. 存在重复元素
  • 用python做的网站多吗二手书交易网站策划书
  • phpcms网站源码ui培训班多少钱
  • Android Studio 导入 opencv
  • 在线网站做成appdede网站地图样式修改
  • 全新尚界H5凭借HUAWEI XMC数字底盘引擎技术,让湿滑路面也“稳”操胜券
  • iOS 26 性能测试实战,如何评估启动速度、CPUGPU 负载、帧率与系统资源适配(uni-app 与 iOS 原生应用性能方案)
  • 腾讯会议→微课操作
  • html原生表格,实现左侧列固定
  • Idea提高开发效率的快捷键最佳学习方式
  • 做网站一定需要icp么中国建设协会官网
  • Selenium使用教程
  • 多线程——单例模式
  • 镜头调焦的 调整的是焦距还是像距?
  • (四)React+.Net+Typescript全栈(错误处理)
  • @ant-design/icons-vue 打包成多格式库
  • 什么是营销型网站?杭州建设教育网站
  • C++开发环境(VSCode + CMake + gdb)
  • JAVA CodeX精选实用代码示例
  • 肥东网站建设南京医院网站建设
  • Qt 多线程解析
  • ZooKeeper与Kafka分布式:从基础原理到集群部署
  • 免费网站服务器安全软件下载wordpress权限设置方法
  • three.js射线拾取点击位置与屏幕坐标映射
  • AutoMQ × Ververica:打造云原生实时数据流最佳实践!
  • Laravel5.8 使用 snappyPDF 生成PDF文件
  • 自己做网站的图片手机芒果tv2016旧版
  • L4 vs L7 负载均衡:彻底理解、对比与实战指南
  • wordpress站群软件自己的网站怎么赚钱
  • 零知IDE——基于STM32F407VET6和MCP2515实现CAN通信与数据采集