import java.util.Stack;

/**
* Source : https://oj.leetcode.com/problems/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.
*/
public class SymmetricTree { /**
* 判断一棵树是否是镜像对称的
*
* 类比判断两棵树是否相同,将一棵树leftNode,rightNode看做两棵树,判断两棵树是否是镜像对称的
*
* 根节点都为空,true
* 只有一个根节点为空,false
* 两个根节点都不为空且相同,递归判断两个子节点
*
* @param left
* @param right
* @return
*/
public boolean isSymmetric (TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if ((left == null && right != null) || (left != null && right == null)) {
return false;
}
if (left.value != right.value) {
return false;
}
return isSymmetric(left.leftChild, right.rightChild) && isSymmetric(left.rightChild, right.leftChild);
} /**
* 使用递归判断是否是镜像对称的
*
* 借助栈实现,
*
* @param left
* @param right
* @return
*/
public boolean isSymmetricByIterator (TreeNode left, TreeNode right) {
Stack<TreeNode> leftStack = new Stack<TreeNode>();
Stack<TreeNode> rightStack = new Stack<TreeNode>();
leftStack.push(left);
rightStack.push(right);
while (leftStack.size() > 0 && rightStack.size() > 0) {
TreeNode leftNode = leftStack.pop();
TreeNode rightNode = rightStack.pop();
if (leftNode == null && rightNode == null) {
continue;
}
if ((leftNode == null && rightNode != null) || (leftNode != null && rightNode == null)) {
return false;
}
if (leftNode.value != rightNode.value) {
return false;
}
leftStack.push(leftNode.leftChild);
leftStack.push(leftNode.rightChild);
rightStack.push(rightNode.rightChild);
rightStack.push(rightNode.leftChild);
}
return true;
} public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() {
}
} public static void main(String[] args) {
SymmetricTree symmetricTree = new SymmetricTree();
char[] treeArr1 = new char[]{'1','2','2','3','4','4','3'};
char[] treeArr2 = new char[]{'1','2','2','#','4','#','4'}; TreeNode tree1 = symmetricTree.createTree(treeArr1);
TreeNode tree2 = symmetricTree.createTree(treeArr2); System.out.println(symmetricTree.isSymmetric(tree1.leftChild, tree1.rightChild) + "----true");
System.out.println(symmetricTree.isSymmetricByIterator(tree1.leftChild, tree1.rightChild) + "----true"); System.out.println(symmetricTree.isSymmetric(tree2.leftChild, tree2.rightChild) + "----false");
System.out.println(symmetricTree.isSymmetricByIterator(tree2.leftChild, tree2.rightChild) + "----false");
} }

leetcode — symmetric-tree的更多相关文章

  1. LeetCode: Symmetric Tree 解题报告

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

  2. [LeetCode] Symmetric Tree 判断对称树

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

  3. [leetcode]Symmetric Tree @ Python

    原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...

  4. LeetCode——Symmetric Tree

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

  5. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

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

  6. [Leetcode] Symmetric tree 对称二叉树

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

  7. Python3解leetcode Symmetric Tree

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

  8. LeetCode() Symmetric Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. leetcode:Symmetric Tree【Python版】

    #error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变 ...

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

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

随机推荐

  1. php 按月创建日志

    public function log($log_string){ //$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."files". ...

  2. cnn神经网络入门

    1.神经网络最基本的原理也是函数拟合,所以lose function就显得非常重要了,我们训练的目的之一就是减小损失函数,常用的损失函数参考:https://www.cnblogs.com/hypnu ...

  3. [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  4. 使用datagrip链接mysql数据库的报错问题.

    1. datagrip刚打开时候,选择风格是白是黑后, 会有一个选择什么数据库,有oracle...一大堆,别选错了.我的是mysql,不要选成了windows sql 和sql. 2 基本设置写完, ...

  5. Linux jdk 环境变量配置

    备忘,引用自:http://blog.csdn.net/lzwglory/article/details/54233248 1. 永久修改,对所有用户有效  # vi /etc/profile //按 ...

  6. 2sat

    之前做的两发 https://vjudge.net/problem/UVALive-3211 #include<cstdio> #include<cstring> #inclu ...

  7. 3-1.Hadoop单机模式安装

    Hadoop单机模式安装 一.实验介绍 1.1 实验内容 hadoop三种安装模式介绍 hadoop单机模式安装 测试安装 1.2 实验知识点 下载解压/环境变量配置 Linux/shell 测试Wo ...

  8. Redis线程模型

    Redis 基于 Reactor 模式开发了自己的网络事件处理器: 这个处理器被称为文件事件处理器(file event handler): 文件事件处理器使用 I/O 多路复用(multiplexi ...

  9. [Swift]LeetCode266.回文全排列 $ Palindrome Permutation

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  10. [Swift]LeetCode808. 分汤 | Soup Servings

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...