leetcode中出现Error - Found cycle in the TreeNode
标题:leetcode中出现Error - Found cycle in the TreeNode
leetcode中出现这种错误,说明你有些地方写错了,写不不够周全
eg:我的方法二和方法三之前就出现了这样的错误,是因为我的left赋为null的位置写的错误造成的
TreeNode pHead = list.get(0);TreeNode node = pHead;node.left = null;for(int i = 1; i < list.size(); i++){node.right = list.get(i);node = node.right;node.left = null;}
/*
方法一:
使用中序非递归
每次得到输出节点时,保存node,让node指向下一次的要输出的节点
if(node != null){node.right = p;
}
node = p;
node.left = null;
执行用时:80 ms, 在所有 Java 提交中击败了5.51% 的用户
内存消耗:43.4 MB, 在所有 Java 提交中击败了99.15% 的用户
*/
public TreeNode inOrder(TreeNode head){if(head == null){return null;}Deque<TreeNode> s = new LinkedList<>();s.push(head);TreeNode node = null;//中序遍历while(!s.isEmpty()){TreeNode p = s.peek();if(p.left != null){s.push(p.left);}else{p = s.pop();System.out.print(p.val + " ");/*if(node == null){node = p;node.left = null;}else{node.right = p;node = p;node.left = null;}*/if(node == null){ //题目要求head = p;}if(node != null){node.right = p;}node = p;node.left = null;while(p.right == null && !s.isEmpty()){p = s.pop();System.out.print(p.val + " ");if(node != null){node.right = p;}node = p;node.left = null;}if(p.right != null){s.push(p.right);}else{break;}}}return head;
}/*
方法二:使用递归中序遍历,得到list,再修改指向 提示一个不知道的错误,Error - Found cycle in the TreeNode
执行用时:5 ms, 在所有 Java 提交中击败了19.29% 的用户
内存消耗:44.1 MB, 在所有 Java 提交中击败了72.87% 的用户
*/
public TreeNode convertBiNode(TreeNode head){if(head == null){return null;}List<TreeNode> list = new ArrayList<>();//初始化listthis.inOrderToList(head, list);/*for(int i = 0; i < list.size() - 1; i++){TreeNode node = list.get(i);node.left = null;node.right = list.get(i + 1);}return list.get(0);*/if(list.size() == 0){return null;}TreeNode pHead = list.get(0);TreeNode node = pHead;node.left = null;for(int i = 1; i < list.size(); i++){node.right = list.get(i);node = node.right;node.left = null;}return pHead;}//中序递归-》list
public void inOrderToList(TreeNode head, List<TreeNode> list){if(head == null){return ;}else{this.inOrderToList(head.left, list);list.add(head);this.inOrderToList(head.right, list);}
}//中序递归遍历,得到的list存储Integer
执行用时:7 ms, 在所有 Java 提交中击败了5.59% 的用户
内存消耗:44.1 MB, 在所有 Java 提交中击败了68.20% 的用户
public TreeNode convertBiNode(TreeNode head){if(head == null){return null;}List<Integer> list = new ArrayList<>();//初始化listthis.inOrderToList(head, list);if(list.size() == 0){return null;}TreeNode pHead = new TreeNode(list.get(0));TreeNode node = pHead;for(int i = 1; i < list.size(); i++){node.right = new TreeNode(list.get(i));node.left = null;node = node.right;}return pHead;}//中序递归-》list
public void inOrderToList(TreeNode head, List<Integer> list){if(head == null){return ;}else{this.inOrderToList(head.left, list);list.add(head.val);this.inOrderToList(head.right, list);}
}/*
方法三:添加全局遍历
使用中序递归 Error - Found cycle in the TreeNode
*/
private TreeNode pHead;
private TreeNode node;
public TreeNode convertBiNode(TreeNode head){this.testConvert(head);return pHead;
}
public void testConvert(TreeNode head){if(head == null){return ;}else {this.testConvert(head.left);if(pHead == null){ //保存需要的头结点pHead = head;}if(node != null){node.right = head;}node = head; //之前写错了node.left = null;this.testConvert(head.right); }
}执行用时:1 ms, 在所有 Java 提交中击败了62.57% 的用户
内存消耗:44.1 MB, 在所有 Java 提交中击败了68.73% 的用户
private TreeNode pHead;
private TreeNode node;
public TreeNode convertBiNode(TreeNode head){this.testConvert(head);return pHead;
}
public void testConvert(TreeNode head){if(head == null){return ;}else {this.testConvert(head.left);if(pHead == null){ //保存需要的头结点pHead = head;}if(node != null){node.right = head;}node = head;node.left = null;this.testConvert(head.right); }
}
leetcode中出现Error - Found cycle in the TreeNode相关推荐
- Go 中的 error 居然可以这样封装
本文由周浩翻译自:https://medium.com/spectro-cloud/decorating-go-error-d1db60bb9249,ironbox和Tang Ruilin参与校对. ...
- 前端开发中的Error以及异常捕获
本文首发于公众号:符合预期的CoyPan 写在前面 在前端项目中,由于JavaScript本身是一个弱类型语言,加上浏览器环境的复杂性,网络问题等等,很容易发生错误.做好网页错误监控,不断优化代码,提 ...
- ZooKeeper配置中出现Error contacting service. It is probably not running
问题描述 ZooKeeper配置中出现Error contacting service. It is probably not running,实际上已经装了ZooKeeper,并进行了相关文件的配置 ...
- 试用版office 2010中提示Error opening SocialConnectorRes.dll求解!!
试用版office 2010中提示Error opening C:\Program Files\Microsoft Office\Office14\2052\SocialConnectorRes.dl ...
- SQL Server 中关于 @@error 的一个小误区
原文:SQL Server 中关于 @@error 的一个小误区 在SQL Server中,我常常会看到有些前辈这样写: if(@@error<>0)ROLLBACK TRANSACTIO ...
- LeetCode中常用语言的一些基本方法记录
文章目录 LeetCode中常用语言的一些基本方法记录 Java 数组 数组的常用操作及方法 Arrays工具类 Collections类常用方法总结 二维数组 字符串常用属性及方法 JavaScri ...
- 中leetcode提示未登录_分享一款将 LeetCode 中 AC 的题目转化为 MarkDown 表格的插件...
背景: 写博客的时候每当新增 LeetCode 题解时都需要在 LeetCode/README 手动更新表格, 非常费劲.因此构思了 crd-leetcode-cli 插件实现自动化同步更新 leet ...
- C++中的“error:LNK2005 已经在*.obj中定义”异常
C++中的"error:LNK2005 已经在*.obj中定义"异常问题 异常现象如下: C++中的"error:LNK2005 已经在*.obj中定义" ...
- Error - Found cycle in the ListNode
Error - Found cycle in the ListNode 刷力扣时遇到这个错误,节点成环 自己摸索了一下发现确实形成循环,原题是206反转链表,我用的是栈,先将链表节点依次进栈,然后依次 ...
最新文章
- leetcode.169 求众数
- linux vga 分辨率低,vga输出 1440x900 分辨率问题
- Android之View和SurfaceView
- LeetCode-Balanced Binary Tree
- 企业存储管理的另一种可能 群晖如何成为NAS代名词?
- hudson linux节点,Linux 环境下搭建 Jenkins(Hudson)平台
- 五.几何对象和空间参考
- Github——git本地仓库建立与远程连接(最详细清晰版本!附简化步骤与常见错误)
- energy in transition课文翻译_思迪软件科技 招聘 字幕翻译(远程兼职)
- WindowsServices_无法拷贝文件到服务器
- Java中的深拷贝与浅拷贝
- Atlas Resources
- c语言正弦波程序_DAC0832的波形信号发生器Proteus仿真设计,正弦波、三角波、方波和锯齿波...
- 如何让Excel输入数据后自动保护,不能被修改
- 2022网络安全技术自学路线图及职业选择方向
- 爬虫:Instagram信息爬取
- cortex系列处理器排行_arm处理器排行_ARM Cortex A系列处理器性能分类比较ARM处理器排名 ZNDS资讯...
- web编程技术基础---CSS
- B树的原理及代码实现、B+树和B*树介绍及应用
- 一种效果很好的自动白平衡技术(WhiteBalance)
热门文章
- 旅行商问题 java_爬山算法(Hill Climbing)解决旅行商问题(TSP)
- 后台返回json数组格式
- poj 3259 时光穿梭问题 bellman_ford算法
- 离职跳槽再启航——三十二岁C++老程序员两年来的心路历程(一)
- django项目实战基于Python实现的衣物捐赠系统
- swift—UIColor十六进制
- 2021年安全月宣教用品
- Oracle 性能优化之AWR、ASH和ADDM(含报告生成和参数解读)
- Cookies 必须启用才能登入 phpmyadmin的问题
- 利用广播星历计算卫星在ECEF下的坐标