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. 1
  2. / \
  3. 2 2
  4. / \ / \
  5. 3 4 4 3

But the following is not:

  1. 1
  2. / \
  3. 2 2
  4. \ \
  5. 3 3

题意就是给定一个二叉树,判定它是否是自己的镜像,这道题我一开始想着只求一下中序序列,然后反着求一下中序序列(右、根、左)两个相等即可。WA了之后,我想了下,某些情况还是不对的,一颗二叉树是自己的镜像,意味着基于根对称,那么求出原来的树的中序、后序,再求出镜像的树的中序、后序,比较一下是否相等即可。当然这种方法不是很好,因为要求四次。

Talk is cheap>>

  1.   public boolean isSymmetric(TreeNode root) {
  2. if (root==null)
  3. return true;
  4. return getMidOrderSeq(root).equals(getReMidOrderSeq(root))&&getPostOrderSeq(root).equals(getRePostOrderSeq(root));
  5. }
  6.  
  7. public String getMidOrderSeq(TreeNode node) {
  8. if (node == null) {
  9. return "";
  10. }
  11. return getMidOrderSeq(node.left) + node.val + getMidOrderSeq(node.right);
  12. }
  13. public String getReMidOrderSeq(TreeNode node) {
  14. if (node == null) {
  15. return "";
  16. }
  17. return getReMidOrderSeq(node.right) + node.val + getReMidOrderSeq(node.left);
  18. }
  19. public String getPostOrderSeq(TreeNode node) {
  20. if (node == null) {
  21. return "";
  22. }
  23. return getPostOrderSeq(node.left) +getPostOrderSeq(node.right)+ node.val ;
  24. }
  25. public String getRePostOrderSeq(TreeNode node) {
  26. if (node == null) {
  27. return "";
  28. }
  29. return getRePostOrderSeq(node.right) +getRePostOrderSeq(node.left)+ node.val ;
  30. }

第二种方法就是用递归,方法参数为两个TreeNode,如果一个为null,检查另一个是否为null,否则就检查这两个节点值是否相等,并递归检查这两个节点的左右子树。

  1.   public boolean isSymmetric(TreeNode root) {
  2. if (root == null)
  3. return true;
  4. return isSymmetric(root, root);
  5.  
  6. }
  7.  
  8. public boolean isSymmetric(TreeNode left, TreeNode right) {
  9. if (left == null || right == null) {
  10. return left == right;
  11. }
  12. return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
  13. }

第三种方法就是用栈来代替递归,定义两个栈,一左一右,分别将左节点的左孩子(左栈)、右节点的右孩子(右栈)和左节点的右孩子(左栈)、右节点的左孩子(右栈)入栈,循环不变式是两个栈都非空,然后取出栈顶元素,比较是否相等(都为空或值相等),不等则直接return false;然后再将这两个左右节点的左右孩子入栈。。。

  1.     public boolean isSymmetricIter(TreeNode root) {
  2. if (root == null || (root.left == null && root.right == null))
  3. return true;
  4. Stack<TreeNode> leftStack = new Stack<>();
  5. Stack<TreeNode> rightStack = new Stack<>();
  6. leftStack.push(root.left);
  7. rightStack.push(root.right);
  8. while (!leftStack.isEmpty() && !rightStack.isEmpty()) {
  9. TreeNode left = leftStack.pop();
  10. TreeNode right = rightStack.pop();
  11. if (left == null && right == null) {
  12. continue;
  13. }
  14. if (left == null || right == null)
  15. return false;
  16. if (left.val != right.val)
  17. return false;
  18. leftStack.push(left.left);
  19. rightStack.push(right.right);
  20. leftStack.push(left.right);
  21. rightStack.push(right.left);
  22. }
  23. return true;
  24. }

Symmetric Tree——LeetCode的更多相关文章

  1. Symmetric Tree [LeetCode]

    Problem description: http://oj.leetcode.com/problems/symmetric-tree/ Basic idea: Both recursive and ...

  2. Symmetric Tree leetcode java

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

  3. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  4. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  5. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  6. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  7. 【leetcode】Symmetric Tree

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

  8. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  9. LeetCode: Symmetric Tree 解题报告

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

随机推荐

  1. Linux如何关闭防火墙和查看防火墙的具体情况

    1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...

  2. ubuntu vim YouComlpeteMe配置

    使用vundle安装时,在.vimrc中添加 Plugin 'Valloric/YouCompleteMe' 使用Bundle会安装失败原因未知 YCM编译时附加选项--system-libclang ...

  3. css中var函数

    引言: 在学习elementui的时候看到一个var.css, 其中写的全部都是以--开头的属性,上google查询不是css3新增的属性,于是决定一探究竟 :root { /* Transition ...

  4. spring 读取properties的两种方法

    一:直接使用context命名空间 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi= ...

  5. urlwrite伪静态(SAE、PHP、JSP)

    在SAE里,直接配置config.yaml文件,文件可以配置的内容包含: 目录默认页面 自定义错误页面 压缩 页面重定向 页面过期 设置响应Header的Content-Type appname: x ...

  6. 【转】mysql数据库中实现内连接、左连接、右连接

    [转]mysql数据库中实现内连接.左连接.右连接 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把对应数据查出来 首先创建数据库中的表,数据库代码如下: /* Navicat MyS ...

  7. 关于android应用闪屏的几种情况

    1.主菜单进入某应用闪屏: 常见是一个空的activity作为launcher属性,实际上它什么事业没干,真正干事情的是从它通过intent启动的activity. 例子: public class ...

  8. 安装php时,make test报错

    Build complete. Don't forget to run 'make test'. [root@localhost php-5.4.5]# make test ERROR: Cannot ...

  9. ORACLE_DBA管理脚本

    SYS @ prod >col index_name for a10 SYS @ prod >col table_name for a10 SYS @ prod >col start ...

  10. JavaScript--Date函数

    1. Date函数 var now = new Date(); 获取当前日期对象 now对象->Date.prototype->Object.prototype 将一个字符串转换为Date ...