关闭

Symmetric Tree

标签: javaleetcodeLinkedList队列
187人阅读 评论(0) 收藏 举报
分类:

题目描述:

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

0
0
查看评论

Leetcode-Symmetric Tree——判断二叉树是否对称

题目 Symmetric Tree  Total Accepted: 12814 Total Submissions: 40358My Submissions Given a binary tree, check whether it is a...
  • disappearedgod
  • disappearedgod
  • 2014-04-20 01:00
  • 2133

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

【101-Symmetric Tree(对称树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its c...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-07 07:29
  • 3813

Symmetric Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/symmetric-tree/  这道题是树的题目,本质上还是树的遍历。这里无所谓哪种遍历方式,只需要对相应结点进行比较即可。一颗树对称其实就是看左右子树是否对称,一句话就是左同右,右同左,结点是对称的相...
  • linhuanmars
  • linhuanmars
  • 2014-04-07 04:52
  • 8526

LeetCode(101)Symmetric Tree

题目如下: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: ...
  • feliciafay
  • feliciafay
  • 2014-01-17 06:48
  • 2937

【leetcode c++】101 Symmetric Tree

Symmetric Tree Given a binary tree, check whether it is amirror of itself (ie, symmetric around its center). For example, this binary tree is symmet...
  • hqq39
  • hqq39
  • 2015-07-27 14:39
  • 325

【LeetCode】101. Symmetric Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. ...
  • crazy1235
  • crazy1235
  • 2016-05-30 23:06
  • 6542

101. Symmetric Tree Leetcode Python

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 ...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-30 07:28
  • 1500

Leetcode Symmetric Tree 递归和非递归解法

和same tree题目差不多一样的解法。 1 recursively 递归法: 2 iteratively 非递归: 使用先序遍历,原来树的先序遍历的非递归写法也不简单。
  • kenden23
  • kenden23
  • 2014-02-16 01:11
  • 14371

101. Symmetric Tree [easy] (Python)

题目链接https://leetcode.com/problems/symmetric-tree/题目原文 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)....
  • coder_orz
  • coder_orz
  • 2016-06-03 15:53
  • 1571

LeetCode--Symmetric Tree

对于本题,想到一个中序遍历后,判别是否为回文串的方法,却WA多次 class Solution { public: vector vectorValue; void inOrder(TreeNode* root) { if(root!=NULL) ...
  • greenapple_shan
  • greenapple_shan
  • 2014-05-16 14:27
  • 680
    个人资料
    • 访问:64062次
    • 积分:2776
    • 等级:
    • 排名:第15296名
    • 原创:215篇
    • 转载:37篇
    • 译文:0篇
    • 评论:6条
    最新评论