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

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

Talk is cheap>>

  public boolean isSymmetric(TreeNode root) {
if (root==null)
return true;
return getMidOrderSeq(root).equals(getReMidOrderSeq(root))&&getPostOrderSeq(root).equals(getRePostOrderSeq(root));
} public String getMidOrderSeq(TreeNode node) {
if (node == null) {
return "";
}
return getMidOrderSeq(node.left) + node.val + getMidOrderSeq(node.right);
}
public String getReMidOrderSeq(TreeNode node) {
if (node == null) {
return "";
}
return getReMidOrderSeq(node.right) + node.val + getReMidOrderSeq(node.left);
}
public String getPostOrderSeq(TreeNode node) {
if (node == null) {
return "";
}
return getPostOrderSeq(node.left) +getPostOrderSeq(node.right)+ node.val ;
}
public String getRePostOrderSeq(TreeNode node) {
if (node == null) {
return "";
}
return getRePostOrderSeq(node.right) +getRePostOrderSeq(node.left)+ node.val ;
}

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

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

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

    public boolean isSymmetricIter(TreeNode root) {
if (root == null || (root.left == null && root.right == null))
return true;
Stack<TreeNode> leftStack = new Stack<>();
Stack<TreeNode> rightStack = new Stack<>();
leftStack.push(root.left);
rightStack.push(root.right);
while (!leftStack.isEmpty() && !rightStack.isEmpty()) {
TreeNode left = leftStack.pop();
TreeNode right = rightStack.pop();
if (left == null && right == null) {
continue;
}
if (left == null || right == null)
return false;
if (left.val != right.val)
return false;
leftStack.push(left.left);
rightStack.push(right.right);
leftStack.push(left.right);
rightStack.push(right.left);
}
return true;
}

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. [转] __thread关键字

    http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量 ...

  2. 设置listview的header不能点击

    View headView = inflater.inflate(R.layout.search_top, null); mListView.addHeaderView(headView ,null, ...

  3. Java基础知识强化之集合框架笔记09:Collection集合迭代器使用的问题探讨

    1.Collection集合迭代器使用的问题探讨: (1)问题1:能用while循环写这个程序,我能不能用for循环呢?                  可以使用for循环替代. (2)问题2:不要 ...

  4. 第五章:最后一步准备,1.8的Json模型、状态描述机制详解

    <基于1.8 Forge的Minecraft mod制作经验分享> 1.8的所有纹理材质都需要一个Json来对其描述,这一块感觉是各大神的教程里面涉及最少最浅的,我就斗胆在这分享下我研究了 ...

  5. codevs 1378 选课 (树形DP)

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; ][],f[] ...

  6. Android VideoView

    这两天公司要让做一个播放视频的小Demo,于是网上学习了下VideoView的使用方法. 先看布局文件,很简单 就是一个VideoView和两个ImageView <RelativeLayout ...

  7. vs2010-error LNK1123: failure during conversion to COFF: file invalid or corrupt

    在项目上右键->Properties-> configuration Properties->Enable Incremental Linking(设置为No). ref: Link ...

  8. POJ 1240 Pre-Post-erous! 解题报告

    题意: 给出一个m叉树的前,后序遍历求这样的树有多少种. Solution: 我们知道前序遍历的第一个点一定是根节点,后序遍历的最后一个点一定是根节点. 由此,我们只一要确定对于每一个节点,它有多少个 ...

  9. Linux进程或线程绑定到CPU

    Linux进程或线程绑定到CPU 为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程. 进程绑定到CPU Linux提供一个接口,可以将进程 ...

  10. javascript Array类型 方法大全

    1,创建数组 //第一种是使用Array构造函数 var colors = new Array(); var colors = new Array(20); //创建length为20的数组 var ...