leetcode — symmetric-tree
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的更多相关文章
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode]Symmetric Tree @ Python
原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...
- LeetCode——Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [Leetcode] Symmetric tree 对称二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode() Symmetric Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 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变 ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
随机推荐
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- C++的编译预处理
C++中,在编译器对源程序进行编译之前,首先要由预处理对程序文本进行预处理.预处理器提供了一组预编译处理指令和预处理操作符.预处理指令实际上不是C++语言的一部分,它只是用来扩充C++程序设计的环境. ...
- 关于H5的一些杂思细想(一)
作为一名前端程序媛,虽然整天和代码打交道,但是还是有一颗小清新的内心,虽然有时候加起班来不是人,但是空闲的时候还是会整理一下思绪,顺便整理一下自己,两个多月的加班,一直没有更新,今天就把自己最近做的一 ...
- NOIP-火柴棒等式
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1. 加号与等 ...
- ES6语法(一)
对于ES6中的一些基础语法,包括对数组/对象/函数/字符串的操作,chroem已经支持了这些语法 // var a = '你' // console.log(a.length) let a = 'ni ...
- NetToPLCSIM 连接PLCSIM 和Kepware 联合仿真
之前只知道PLCSIM 单独仿真调试PLC的程序,后来接触KepwareOPC,想着如果能够仿真PLC和Kepware的通信,更加方便调试.于是在网上搜索了一下,发现NetToPLCSIM这个软件. ...
- DIV滚动条滚动到指定位置(jquery的position()与offset()方法区别小记)
相对浏览器,将指定div滚到到指定位置,其用法如下 $("html,body").animate({scrollTop: $(obj).offset().top},speed); ...
- windows10下Kafka环境搭建
内容小白,包含JDK+Zookeeper+Kafka三部分.JDK:1) 安装包:Java SE Development Kit 9.0.1 下载地址:http://www.oracle ...
- 去掉input在type="number"时右边的上下箭头
加了代码之后: input::-webkit-outer-spin-button, input::-webkit-inner-spin-button{ -webkit-appearance: none ...
- CSS Media媒体查询使用大全,完整媒体查询总结
前面的话 一说到响应式设计,肯定离不开媒体查询media.一般认为媒体查询是CSS3的新增内容,实际上CSS2已经存在了,CSS3新增了媒体属性和使用场景(IE8-浏览器不支持).本文将详细介绍媒体查 ...