LeetCode OJ: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
判断一颗树是否对称,首先用递归的方法当然比较容易解决:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL)
return true;
if(!root->left && !root->right)
return true;
if(root->left && !root->right || !root->left && root->right)
return false;
return checkSymmetric(root->left, root->right);
} bool checkSymmetric(TreeNode * left, TreeNode * right)
{
if(!left && !right)
return true;
if(!left && right || left && !right)
return false;
if(left->val != right->val)
return false;
return checkSymmetric(left->left, right->right) && checkSymmetric(left->right, right->left);
}
};
题目还要求用非递归的方式来实现,制造两个队列就可以实现了:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode *> q1;
queue<TreeNode *> q2;
if(root == NULL) return true;
if(!root->left && !root->right) return true;
if(root->left && !root->right || !root->left && root->right) return false;//val
q1.push(root->left);
q2.push(root->right);
TreeNode * tmpLeft, *tmpRight;
while(!q1.empty() && !q2.empty()){
tmpLeft = q1.front();
tmpRight = q2.front();
q1.pop(), q2.pop();
if(!tmpLeft && !tmpRight)
continue;
if(!tmpLeft && tmpRight || tmpLeft && !tmpRight)
return false;
if(tmpLeft->val != tmpRight->val)
return false;
q1.push(tmpLeft->left);
q1.push(tmpLeft->right);
q2.push(tmpRight->right);
q2.push(tmpRight->left);
}
return q1.empty() && q2.empty();
}
};
java的递归版本如下所示:
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
if(root.left == null && root.right == null)
return true;
if(root.left != null && root.right != null)
return checkSymmetric(root.left, root.right);
return false;
}
public boolean checkSymmetric(TreeNode leftNode, TreeNode rightNode){
if(leftNode == null && rightNode == null)
return true;
if(leftNode != null && rightNode != null){
if(leftNode.val == rightNode.val){
return checkSymmetric(leftNode.left, rightNode.right) &&
checkSymmetric(leftNode.right, rightNode.left);
}
else
return false;
}
return false;
}
}
下面是迭代版本,和上面的基本上一样:
public class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q1 = new LinkedList<TreeNode>();
Queue<TreeNode> q2 = new LinkedList<TreeNode>();
TreeNode p1, p2;
if(root == null)
return true;
q1.add(root.left);
q2.add(root.right);
while(!q1.isEmpty() && !q2.isEmpty()){
p1 = q1.poll();
p2 = q2.poll();
if(p1 == null && p2 == null)
continue;
if(p1 != null && p2 != null){
if(p1.val == p2.val){
q1.add(p1.left);
q1.add(p1.right);
q2.add(p2.right);
q2.add(p2.left);
}else
return false;
}else
return false;
}
return true;
}
}
LeetCode OJ:Symmetric Tree(对称的树)的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode OJ] Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- Symmetric Tree 对称树
判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
随机推荐
- re模块(Python中的正则表达式)
re模块 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎 ...
- Protobuf支持 pointf
Protobuf支持 pointf序列化 加入:ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(System.Drawing.PointF), fa ...
- LeetCode:数据库技术【180-185】
LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...
- text_field text_tag 用法
= f.text_field :tax_category_id, :value => @invoice.tax_category.name, :class => "form-co ...
- margin无法居中原因
1.要给居中的元素一个宽度,否者无效. 2.该元素一定不能浮动,否者无效. 3 在HTML中使用标签,需考虑好整体构架,否者全部元素都会居中的.
- Yii2.0数据库查询实例(三)
常用查询: // WHERE admin_id >= 10 LIMIT 0,10 User::find()->])->offset()->limit()->all() / ...
- UI设计中的各种小控件
xib支持图形化操作,提供了几乎所有的控件可供选择,只需拖动到相应的位置即可,但是控件的后台代码仍然需要手动编写,一定程度上加速了前台的开发. xib快速开发程序,手写代码速度比较慢 xib适合做静态 ...
- 转:CWebBrowser2去除边框、滚动条、右键菜单
http://blog.csdn.net/tangyin025/article/details/8675513 添加CWebBrowser2类 右键项目-〉Add-〉Class...-〉MFC-〉MF ...
- 每天一个Linux命令(62)rcp命令
rcp代表"remote file copy"(远程文件拷贝). (1)用法: 用法: rcp [参数] [源文件] [目标文件] (2)功能: ...
- $《第一行代码:Android》读书笔记——第1章 Android系统
(一)Android系统架构 1.Linux内核层:各种底层驱动,如显示驱动.音频驱动.电源管理等. 2.系统运行库层:各种库支持,如3D绘图.浏览器内核.数据库等. 3.应用框架层:各种API,各种 ...