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<>();
	//初始化list
	this.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<>();
	//初始化list
	this.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);	
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值