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. HTTP协议详解之User Agent篇

    •User Agent:用户代理 指浏览器他的信息包括硬件平台.系统软件.应用软件和用户个人偏好.用户代理不仅仅指浏览器,还包括搜索引擎. •为什么所有浏览器的User Agent都带有Mozilla ...

  2. 微信小程序“信用卡还款”项目实践

    小程序概述 11月3日晚,微信团队对外宣布,微信小程序开放公测.开发者可登陆微信公众平台申请,开发完成后可以提交审核,公测期间暂不能发布. 我们前一段时间也进行了小程序开发,现在来对之前的开发体验做一 ...

  3. 关于 while(1)和for(;;)效率问题的一点想法

    这几天看到好友的文章关于while(1)和for(;;)效率的讨论,手痒说了两句.回头一寻思,自己也仅仅是判断.没有做不论什么实验,我们就看看这两种写法究竟有什么差别: 实验环境:IAR EWARM ...

  4. Mybatis日期类型的关系判断

    进行时间段的查询时,在mapper文件中直接使用">","<"等关系运算符是无法解析的 <if test="executeStart ...

  5. HDUOJ----4504 威威猫系列故事——篮球梦

    威威猫系列故事——篮球梦 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  6. SQL Server因为数据库正在使用,所以无法获得对数据库的独占访问权

    恢复数据库: 恢复数据库之前,先执行下面这句话 ALTER DATABASE [mpn_stat] SET OFFLINE WITH ROLLBACK IMMEDIATE 执行恢复数据库SQL语句 R ...

  7. INFORMATION_SCHEMA数据库介绍

    删除mysql数据库某一张主键表的所有外键关系 SELECT CONCAT('alter table ', TABLE_NAME , ' drop foreign key ', constraint_ ...

  8. WebDav的java客户端开发包:sardine

    最近需要对WebDav服务器进行操作,查找了一下,基于java的开发包主要有这几个: slide Jackrabbit sardine webdavclient4j 其中slide是apache的一个 ...

  9. Unix环境高级编程(十八)高级进程间通信

    本章主要介绍了基于STREAM的管道和UNIX域套接字,这些IPC可以在进程间传送打开文件描述符.服务进程可以使用它们的打开文件描述符与指定的名字相关联,客户进程可以使用这些名字与服务器进程通信. 1 ...

  10. iOS 关于 设计模式 与网友讨论实录

    关于 设计模式 与网友讨论实录 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留 ...