Symmetric Tree

本文详细介绍了如何通过递归和迭代方法解决二叉树对称性判断问题,并提供了AC代码实现。同时,文章指出中序遍历对称并不能保证原始树的对称性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.


这个题用递归比较好处理,代码如下:

public boolean isSymmetric(TreeNode root) {
	if(root==null)
		return true;
	return BothIsSymmetric(root.left, root.right);
}

public boolean BothIsSymmetric(TreeNode root1,TreeNode root2) {
	if(root1==null&&root2==null)
		return true;
	if((root1==null^root2==null)||(root1!=null&&root2!=null&&root1.val!=root2.val))
		return false;
	return BothIsSymmetric(root1.left, root2.right)&&BothIsSymmetric(root1.right, root2.left);
}
关键是用迭代的方法去做

有的人说如果中序遍历之后对称那么原来的树就是镜像的,这简直就是误人子弟嘛。

因为[1,2,3,3,null,2,null]这种就不是镜像的!

然后看了其他人的做法,采用队列的方法去做。

但是我用的是ArrayDeque,这种offer,add,addLast方法(本质上都是调用addLast方法),都不能加入null值,导致程序出现各种各样的BUG,太浪费时间了。以后还是要多熟悉熟悉这些数据结构的源代码啊

enum Child{
	left,right;
}

class NewNode{
	TreeNode root;
	TreeNode parent;
	Child child;
	
	public NewNode(TreeNode root,TreeNode parent,Child child){
		this.root=root;
		this.parent=parent;
		this.child=child;
	}
}
public boolean isSymmetric(TreeNode root) {
	if(root==null)
		return true;
	Deque<NewNode> leftQueue=new ArrayDeque<NewNode>();
	Deque<NewNode> rightQueue=new ArrayDeque<NewNode>();
	if(root.left!=null)
		leftQueue.offer(new NewNode(root.left, root, Child.left));
	if(root.right!=null)
		rightQueue.offer(new NewNode(root.right, root, Child.right));
	while(!leftQueue.isEmpty()&&!rightQueue.isEmpty()){
		NewNode leftNewNode=leftQueue.poll();
		NewNode rightNewNode=rightQueue.poll();
		if(leftNewNode.root.val!=rightNewNode.root.val||leftNewNode.child==rightNewNode.child||leftNewNode.parent!=rightNewNode.parent)
			return false;
		if(leftNewNode.root.left!=null)
			leftQueue.offer(new NewNode(leftNewNode.root.left,leftNewNode.root,Child.left));
		if(leftNewNode.root.right!=null)
			leftQueue.offer(new NewNode(leftNewNode.root.right,leftNewNode.root,Child.right));
		if(rightNewNode.root.right!=null)
			rightQueue.offer(new NewNode(rightNewNode.root.right,rightNewNode.root,Child.right));
		if(rightNewNode.root.left!=null)
			rightQueue.offer(new NewNode(rightNewNode.root.left,rightNewNode.root,Child.left));
	}
	if(leftQueue.isEmpty()&&rightQueue.isEmpty())
		return true;
	return false;
}
于是我换成了LinkedList,这里的LinkedList使用的是双指针,实现了堆栈和队列,且可以加入null值。

正确迭代AC代码如下:

public boolean isSymmetric(TreeNode root) {
	if(root==null)
		return true;
	LinkedList<TreeNode> leftQueue=new LinkedList<TreeNode>();
	LinkedList<TreeNode> rightQueue=new LinkedList<TreeNode>();
	leftQueue.push(root.left);
	rightQueue.push(root.right);
	while(!leftQueue.isEmpty()&&!rightQueue.isEmpty()){
		TreeNode leftnode=leftQueue.poll();
		TreeNode rightnode=rightQueue.poll();
		if(leftnode==null&&rightnode==null)
			continue;
		if(leftnode==null||rightnode==null)
			return false;
		if(leftnode.val!=rightnode.val)
			return false;
		leftQueue.push(leftnode.left);
		leftQueue.push(leftnode.right);
		rightQueue.push(rightnode.right);
		rightQueue.push(rightnode.left);
	}
	if(leftQueue.isEmpty()&&rightQueue.isEmpty())
		return true;
	return false;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值