Symmetric Tree,对称树
2016-09-24 18:57
309 查看
问题描述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree
But the following
算法分析:和same tree比较像,都可以用递归方法来解决。
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); } } }
相关文章推荐
- 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
- java编译和运行的过程
- Android App签名(证书)校验过程源码分析
- python笔试题之找出一个列表里出现频次最高的元素(most common elements in a list)
- Caffe + Ubuntu 15.04 + CUDA 7.5 新手安装配置指南
- 知乎日报客户端
- GP索引调优测试--基本篇
- MySQL优化聊两句
- 用Fedora构建自己的Linux发行版
- 【App推广】国内App推广终极37个方法
- Mac note -one
- 微信应用号这滩“浑水”,互联网创业者们到底要不要蹚?
- 你不知道的Java学习笔记13--Date类
- 链表奇偶排序以及反转问题
- eclipse color theme chose
- Sublime Text3 Package Control 在菜单栏中不显示
- leetcode 11.Container With Most Water
- Oracle在Windows cmd上显示调整
- 北京赛区(2016)网络赛 题目1 : The Book List 【字典树】
- 寄存器