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; ...
随机推荐
- [git]checkout&branch
git branch 和 git checkout经常在一起使用,所以在此将它们合在一起 1.Git branch 一般用于分支的操作,比如创建分支,查看分支等等, 1.1 git branch 不带 ...
- BZOJ1431 : MLand
考虑任意一棵生成树,它的代价是一个一次函数. 因此所有生成树的最小值随着时间变化呈现出的是一个上凸壳. 三分查找最大值即可. 时间复杂度$O(m\log m\log w)$. #include< ...
- 微信跳转,wap网页跳转微信打开指定页面
最近一朋友的客户有这么一需求,wap网页跳转微信打开指定页面,让用户去关注公众号.想这么干,通过网页跳转到微信并打开带有二维码的网页,用户长按识别二维码就可以关注了.想法很好,于是去搜索了“微信跳转, ...
- P2649 - 【NOIP2017】列队
Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n×m 名学生,方阵的行数为 ...
- ECS云服务器配置数据库远程链接
环境: windows server 2012 R2 Datacenter(数据中心版本) sql server 2008 R2 Express (环境的安装步骤此处不做教程) 开启远端链接重要步骤如 ...
- pip离线安装依赖包
pip安装离线本地包 导出本地已有的依赖包 pip freeze > requirements.txt 将依赖包下载到本地 # 下载到当前目录,指定pip源 pip download -r re ...
- Java内存模型锦集
[内存操作与内存屏障] 内存模型操作: lock(锁定) : 作用与主内存的变量, 它把一个变量标识为一条线程独占的状态 unlock(解锁) : 作用于主内存变量, 它把一个处于锁定状态的变量释放出 ...
- Java作业十(2017-11-8)
public class TAutoPerson { public static void main(String args[]) { new TAutoPerson().fun(); } publi ...
- DDD事件总线的实现
基本思路: (1) 在事件总线内部维护着一个事件与事件处理程序相映射的字典. (2) 利用反射,事件总线会将实现了IEventHandler的处理程序与相应事件关联到一起,相当 ...
- Java高并发缓存架构,缓存雪崩、缓存穿透之谜
面试题 了解什么是 redis 的雪崩.穿透和击穿?redis 崩溃之后会怎么样?系统该如何应对这种情况?如何处理 redis 的穿透? 面试官心理分析 其实这是问到缓存必问的,因为缓存雪崩和穿透,是 ...