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; ...
随机推荐
- eclipse自身导致的项目问题:上边提示需要移除无用包,下边类提示需要导入包。
- RSP小组——消消乐
RSP小组--消消乐 团队所有博客总结 1.团队第一周作业 2.团队第二周作业 3.RSP小组--团队冲刺博客一 4.RSP小组--团队冲刺博客二 5.RSP小组--团队冲刺博客三 6.RSP小组-- ...
- Centos 7安装python3(PY3.6)
# 安装 sudo yum install centos-release-scl sudo yum install rh-python36 #开启 scl enable rh-python36 bas ...
- c# asp.net mvc使用斑马GK888t打印机打印标签
前言 c#语言,asp.net mvc,南京都昌电子病历模板工具(类似word),斑马GK888t,打印手腕带和标签纸. 实现步骤为:在页面上显示一个或多个都昌模板工具,点击页面上的button,出现 ...
- Android SQLite数据库升级,怎么做(事物更改)
SQLiteOpenHelper // 如果数据库文件不存在,只有onCreate()被调用(该方法在创建数据库时被调用一次) public abstract void onCreate(SQLite ...
- typeof获取变量的数据类型 javascript
获取变量的数据类型:typeof <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 漏测BUG LIst
5. 接口设计问题 - 主从存在延时,当两个接口需要一个主库,一个从库的时候,可能会出问题,时时性 4. 开发的接口文档也得进行简单的测试,根据产品文档/业务测试接口(针对问题2) 3. 需要上的课 ...
- centos 踩坑集锦
定时任务 top 命令添加定时任务无效 我通过以下命令获取总进程数与僵尸进程数 vim procs.sh procs_total=`/bin/top -n 1|grep Tasks|sed 's/,/ ...
- VUE 出现Access to XMLHttpRequest at 'http://192.168.88.228/login/Login?phone=19939306484&password=111' from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 'Access-Contr
报错如上图!!!! 解决办法首先打开 config -> index.js ,粘贴 如下图代码,'https://www.baidu.com'换成要访问的的api域名,注意只要域名就够了, ...
- JAVA---MYSQL 基本知识点 第二部分
增删改查 (CRUD): 数据库 , 表 , 记录 ; 约束 ; 主键约束 :primary key 如果是int类型 可以使用 自动增长型 auto_increment; 唯一约束 ...