问题描述:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

    1
/ \
2 2
\ \
3 3

算法分析:和same tree比较像,都可以用递归方法来解决。

public class SymmeticTree
{
public boolean isSymmetric(TreeNode root)
{
if (root == null)
{
return true;
}
return isSymmetric(root.left, root.right);
} public boolean isSymmetric(TreeNode left, TreeNode right)
{
if (left == null && right == null)
{
return true;
}
else if ((left == null && right != null)
|| (right == null && left != null) || (left.val != right.val))
{
return false;
}
else
{
return isSymmetric(left.left, right.right)
&& isSymmetric(left.right, right.left);
}
}
}

最新文章

  1. 惊心动魄的一上午,感谢eclipse 的文件恢复功能
  2. Tree树节点选中及取消和指定节点的隐藏
  3. 【原】iphone6来了,我该做点什么(兼容iphone6的方法)
  4. 【总结1】PhpStorm配置XDebug(远程)调试PHP
  5. JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
  6. 【Hello CC.NET】自动化发布时 Web.config 文件维护
  7. Type mismatch: cannot convert from MainFragment to Fragment 报错
  8. EPLAN P8导线颜色的设置
  9. shell script 学习笔记-----shell变量
  10. android 点击edittext弹出软键盘,否则不弹
  11. FMX的综合评价
  12. xml、txt、config的一些基本用法
  13. linux 修改IP, DNS 命令
  14. Mac上利用Eclipse编译Cocos2d-x
  15. v3学院带你一次性认清UART、RS-232、RS-422、RS-485的区别
  16. C#简单入门
  17. 真win10官方原版ISO下载方法
  18. Myeclipse2017C版本破解
  19. JSLinux
  20. Linux kernel Programming - Concurrency and Race Conditions

热门文章

  1. NodeJS收发GET和POST请求
  2. 微信小程序 --- model弹框
  3. 微信小程序 --- page.js文件
  4. java利用poi 把ppt转化为图片,
  5. 修改js confirm alert 提示框文字
  6. JSP教程
  7. postgresql----TEMPORARY TABLE和UNLOGGED TABLE
  8. [ASP.NET 大牛之路]03 - C#高级知识点概要(2) - 线程和并发
  9. proc_create函数内幕初探
  10. neovim 使用