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:

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.

SOL 1 & 2:

两个解法,递归和非递归.

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// solution 1:
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
} return isSymmetricTree(root.left, root.right);
} /*
* 判断两个树是否互相镜像
(1) 根必须同时为空,或是同时不为空
*
* 如果根不为空:
(1).根的值一样
(2).r1的左树是r2的右树的镜像
(3).r1的右树是r2的左树的镜像
*/
public boolean isSymmetricTree1(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
} if (root1 == null || root2 == null) {
return false;
} return root1.val == root2.val &&
isSymmetricTree(root1.left, root2.right) &&
isSymmetricTree(root1.right, root2.left);
} // solution 2:
public boolean isSymmetricTree(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
} if (root1 == null || root2 == null) {
return false;
} Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>(); // 一定记得初始化
s1.push(root1);
s2.push(root2); while (!s1.isEmpty() && !s2.isEmpty()) {
TreeNode cur1 = s1.pop();
TreeNode cur2 = s2.pop(); if (cur1.val != cur2.val) {
return false;
} if (cur1.left != null && cur2.right != null) {
s1.push(cur1.left);
s2.push(cur2.right);
} else if (!(cur1.left == null && cur2.right == null)) {
return false;
} if (cur1.right != null && cur2.left != null) {
s1.push(cur1.right);
s2.push(cur2.left);
} else if (!(cur1.right == null && cur2.left == null)) {
return false;
}
} return true;
}
}

2015.1.20:

简化了递归的解法,root与root本身是一对对称树。

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
} public boolean isMirror(TreeNode r1, TreeNode r2) {
if (r1 == null && r2 == null) {
return true;
} else if (r1 == null || r2 == null) {
return false;
} return r1.val == r2.val
&& isMirror(r1.left, r2.right)
&& isMirror(r1.right, r2.left);
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSymmetric.java

LeetCode: Symmetric Tree 解题报告的更多相关文章

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  4. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  5. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  7. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  8. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  9. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

随机推荐

  1. JqGrid获得所有选中行数据ID数组,获取所有行的ID数组

    获得选中行的ID数组:var ids = $("jqgridtableid").jqGrid('getGridParam','selarrrow'); 获得所有行的ID数组:var ...

  2. DIV布局之position详解

    相对定位和绝对定位 定位标签:position 包含属性:relative(相对) absolute(绝对) 1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在 ...

  3. 老毛桃pe装机工具备份系统

    电脑故障可以说是难以避免的,误操作或者修改了哪个设置系统就莫名其妙崩溃了.这在日常使用当中并不鲜见,许多用户就会寻求备份系统方法.有没有好的一键备份系统教程可以参考呢?在本篇教程中,就容我跟大家讲讲怎 ...

  4. HDUOJ-------(1022)Train Problem I

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. 为同一部电脑设置2个IP地址

    为同一部电脑设置2个IP地址 在HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\NetTrans下 点击0000.0001,000 ...

  6. Python基本数据类型详细介绍(转)

    1.空(None)表示该值是一个空对象,空值是Python里一个特殊的值,用None表示.None不能理解为0,因为0是有意义的,而None是一个特殊的空值.2.布尔类型(Boolean)在 Pyth ...

  7. JS阻止冒泡方法(转)

    S事件流其中一种是冒泡事件,当一个元素被触发一个事件时,该目标元素的事件会优先被执行,然后向外传播到每个祖先元素,恰如水里的一个泡泡似的,从产生就一直往上浮,到在水平面时,它才消失.在这个过程中,如果 ...

  8. Android学习系列(9)--App列表之分组ListView

    吸引用户的眼球,是我们至死不渝的追求:      第一时间呈现最有价值的信息,简明大方,告诉客户,你的选择是多么的明智,这正是你寻觅已久的东西.       分组的应用场合还是很多的,有数据集合的地方 ...

  9. RabbitMQ消息队列(五):Routing 消息路由[转]

    上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity(严重程度)来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindin ...

  10. http请求No peer certificate的解决方法

    不少同学在做HTTP请求新浪授权或新浪数据的时候会出现 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate的异常.现给出解决方法 ...