38. Same Tree && Symmetric Tree
Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
思想: 无。能遍历即可。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if((p == NULL && q) || (p && q == NULL)) return false;
if(p == NULL && q == NULL) return true;
if(p->val != q->val) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
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.
思想: 构造其镜像树。
1. 递归。用 Same Tree方法判断即可。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode* getMirror(TreeNode *root) {
if(root == NULL) return NULL;
TreeNode *p = new TreeNode(root->val);
p->left = getMirror(root->right);
p->right = getMirror(root->left);
return p;
}
bool isSymmetricCore(TreeNode *root, TreeNode *root2) {
if((!root && root2) || (root && !root2)) return false;
if(!root && !root2) return true;
if(root->val != root2->val) return false;
return isSymmetricCore(root->left, root2->left) && isSymmetricCore(root->right, root2->right);
}
class Solution {
public:
bool isSymmetric(TreeNode *root) {
TreeNode *mirrorTree = getMirror(root);
return isSymmetricCore(root, mirrorTree);
}
};
2. 迭代。两棵树相同方法遍历即可。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode* getMirror(TreeNode *root) {
if(root == NULL) return NULL;
TreeNode *p = new TreeNode(root->val);
p->left = getMirror(root->right);
p->right = getMirror(root->left);
return p;
}
bool pushChildNode(TreeNode *p1, TreeNode *p2, queue<TreeNode*> & qu, queue<TreeNode*>& qu2) {
if(p1->left && p2->left) { qu.push(p1->left); qu2.push(p2->left); }
else if(p1->left || p2->left) return false;
if(p1->right && p2->right) { qu.push(p1->right); qu2.push(p2->right);}
else if(p1->right || p2->right) return false;
return true;
}
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(root == NULL) return true;
TreeNode *mirrorTree = getMirror(root);
queue<TreeNode*> que1;
queue<TreeNode*> que2;
que1.push(root);
que2.push(mirrorTree);
while(!que1.empty() && !que2.empty()) {
TreeNode *p1 = que1.front(), *p2 = que2.front();
que1.pop(); que2.pop();
if(p1->val != p2->val) return false;
if(!pushChildNode(p1, p2, que1, que2)) return false;
}
if(!que1.empty() || !que2.empty()) return false;
return true; }
};
38. Same Tree && Symmetric Tree的更多相关文章
- 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- 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 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
随机推荐
- longitude
确保有loc:[longitude, latitude]属性给loc增加索引AttractionSchema.index({loc: '2d'});使用geoNeardb.places.find( { ...
- android studio 改变代码提示的方法
移通152余继彪 在android studio中 默认代码提示的功能是ctrl+空格,这样的提示会和输入法造成冲突,所以要改变 改变的方法就是file—seting——Keymap然后搜索basic ...
- android 面试总结,后续注意学习
1.数据结构和算法 一般情况下,工作中是用不到的,但是就是问,每个公司都问,而且有的公司还问的特别深,还让你手写代码.我觉得这个确实有点恶心,我个人也是很讨厌算法的,但是真是没办法,人家就问,你说怎么 ...
- Certificate、Provisioning Profile、App ID
关于 Certificate.Provisioning Profile.App ID 的介绍及其之间的关系 2014-03-13 15:26 13416人阅读 评论(1) 收藏 举报 目录(?)[ ...
- Flask生成SECRET_KEY(密钥)的一种简单方法
SECRET_KEY是Flask中比较重要的一个配置值.本文介绍一种比较简单的生成SECRET_KEY的方法. Session, Cookies以及一些第三方扩展都会用到SECRET_KEY值,这是一 ...
- 北京网赛I题 hiho1391 (树状数组、区间覆盖最大值问题)
题目链接:http://hihocoder.com/problemset/problem/1391 题意:A国和B国向对方分别投射N枚和M枚导弹(发射时间,飞行时间,伤害值),同时两国各自都有防御系统 ...
- C#代码示例_定义类
默认情况下,类声明为内部的,即只有当前项目中的代码才能访问它.可以使用internal访问修饰符关键字显示指定. 除了两个访问修饰符关键字(public, internal)外,还可以指定类是抽象的( ...
- 大数据批量插入数据库使用(SqlBulkCopy )效率更高
SqlBulkCopy类是System.Data.SqlClient下的类,我们开发中不常用,甚至不知道有这么一个类的存在,但确实比sql插入,事务批量插入,sql批量拼接插入快很多,比调用存储过程插 ...
- nat转换
实验目的: (1) 了解nat转换 (2) 了解nat转换配置命令 (3) 了解哪些是私有ip地址哪些不是私有ip地址 实验工具: 华为eNSP模拟器和Wireshar 实验拓 ...
- VHDL:信号、端口以及和Verilog的区别
1.信号 信号是描述硬件系统的基本数据对象,它的性质类似于连接线.信号可以作为设计实 体中并行语句模块间的信息交流通道. 信号作为一种数值容器,不但可以容纳当前值,也可以保持历史值(这决定于 ...