Symmetric Tree
2015-08-02 08:05
447 查看
题目:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
The above binary tree is serialized as
题解:
判断左右子树是否对称。
解法一:递归的解法:
解法二:非递归解法:
reference:
http://blog.csdn.net/linhuanmars/article/details/23072829
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.
confused what
"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5
The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}".
题解:
判断左右子树是否对称。
解法一:递归的解法:
public boolean isSymmetricTree(TreeNode p,TreeNode q){ if(p == null&&q == null) return true; if(p == null||q == null) return false; return (p.val == q.val) && isSymmetricTree(p.left, q.right) && isSymmetricTree(p.right, q.left); } public boolean isSymmetric(TreeNode root) { if(root==null) return true; return isSymmetricTree(root.left,root.right); }
解法二:非递归解法:
public boolean isSymmetric(TreeNode root) { if(root == null) return true; if(root.left == null && root.right == null) return true; if(root.left == null || root.right == null) return false; LinkedList<TreeNode> q1 = new LinkedList<TreeNode>(); LinkedList<TreeNode> q2 = new LinkedList<TreeNode>(); q1.add(root.left); q2.add(root.right); while(!q1.isEmpty() && !q2.isEmpty()){ TreeNode n1 = q1.poll(); TreeNode n2 = q2.poll(); if(n1.val != n2.val) return false; if((n1.left == null && n2.right != null) || (n1.left != null && n2.right == null)) return false; if((n1.right == null && n2.left != null) || (n1.right != null && n2.left == null)) return false; if(n1.left != null && n2.right != null){ q1.add(n1.left); q2.add(n2.right); } if(n1.right != null && n2.left != null){ q1.add(n1.right); q2.add(n2.left); } } return true; }
reference:
http://blog.csdn.net/linhuanmars/article/details/23072829
相关文章推荐
- SQL Server里因丢失索引造成的死锁
- 源代码管理工具 SVN
- 定制Eclipse IDE之插件篇(二)
- 社説 20150802 最低賃金上げ 中小企業の体力強化が重要だ
- 日经社説 20150802 TPP交渉を漂流させずに早期決着を
- 日经社説 20150802 限界に近づく救急車の出動
- 日经春秋 20150802
- yarn架构-概述
- PROBLEM_J: Gym 100345J - Zen Garden
- 如何解决ADB server didn't ACK
- 进程的调用
- 进程的调用
- 天声人語 20150802
- RESTful
- docker中建立私有git服务器[gitlab]
- [Drools]JAVA规则引擎 -- Drools
- SGU-154 Factorial (末尾0 & 三分)
- 7个睡前习惯 让你多活10年
- C程序变量的声明、定义、初始化和赋值
- MatLab Load cv::Mat 导入数据