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 ...
随机推荐
- IT行业的斗争
有朋友和我说,她希望做一名IT工作者,原因是可以对着机器工作,不需要再面对人与人之间的是是非非. 我真心不想打破她的憧憬,因为无论干任何行业,有人,就有矛盾,就有斗争. 我是那名失败者,仅仅想安安稳稳 ...
- input标签实现让光标不出现!
实现不让光标出现的效果,很好用,例子如下: <input class="red" readonly unselectable="on" value=&qu ...
- 修改input的type属性
在ff和chrome中是可以直接修改input的type属性的,但是在ie下面是不允许的. 用jquery 的attr方法去修改,在jquery1.8.3版本会直接抛出异常,但在1.9及以上版本就不再 ...
- Python使用CGIHTTPServer调用shell作为cgi脚本
#!/bin/bash echo "Content-Type:text/html" echo "" echo "hello world!" ...
- Hubilder快捷键
/* tips.txt的内容可以在HBuilder启动封面中显示.你可以自定义一个有趣的启动tips,增加[user]开头的文本内容即可.比如[user]我要减肥! */ 你按照Hello HBui ...
- Unity Sprite转Prefab
新项目使用Unity5.X,遇到了一些问题,其中就有Sprite的管理更新问题,查了一些资料,Mono推荐的是转为Prefab处理. 看了一些国外同行的处理方法,分析了一个编辑器插件脚本.学到了一些技 ...
- iOS常用设计模式和机制之Block简单使用
Block :block 实际上就是 Objective-C语言对闭包的实现 闭包(Closure):闭包就是一个函数,或者一个指向函数的指针,加上这个函数执行的非局部变量.闭包允许一个函数访问声明该 ...
- 如何在一个网站或者一个页面规划JS
规划主要分为两部分:1.JS的分层,2.Js的规划 1.JS的分层(功能) 1-1.底层的库 : jquery 1-2.组件(ui) : 比如拖拽等,模块之间没有必然的联系,可以重复利用 1-3. ...
- python是一个解释器
python是一个解释器 利用pip安装python插件的时候,观察到python的运作方式是逐步解释执行的 适合作为高级调度语言: 异常的处理以及效率应该是主要的问题
- 什么是JavaEE
Java技术不仅是一门编程语言而且是一个平台.同时Java语言是一门有着特定语法和风格的高级的面向对象的语言,Java平台是Java语言编写的特定应用程序运行的环境.Java平台有很多种,很多的Jav ...