Symmetric Tree

来源:互联网 发布:51单片机指令集ppt 编辑:程序博客网 时间:2024/04/20 01:40

题目描述:

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