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的更多相关文章

  1. 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...

  2. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  3. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  4. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  5. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  6. LeetCode: Symmetric Tree 解题报告

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  7. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  8. 【LeetCode】101. Symmetric Tree (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  9. &lt;LeetCode OJ&gt; 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

随机推荐

  1. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  2. php常量的声明和使用

    (1)声明 define("var",value) (2)先声明后使用 (3)默认区分大小写,如要不区分 define ("var",value,true); ...

  3. 正则表达式 java

    如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单.如果你不熟悉这个术语,那么"正则表达式"(Regular Expres ...

  4. asp.net导出word(word2007)

    1.只能导出成word2007格式(.docx),可直接导出到客户端 2.服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片) 3.需要一个国外的DocX.dll插件 4.需要添加引 ...

  5. hibernate的数据库乱码问题

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration ...

  6. Zyxel Switch-How to block a fake DHCP server without enabling DHCP snooping?

    How to block a fake DHCP server without enabling DHCP snooping? Scenario How to block a fake DHCP se ...

  7. How to browse the entire documentation using XCode 5 Documentation and API Reference ?

    file:///Users/yangiori/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.Ap ...

  8. JQuery Jsonp 跨域

    需求:两个不同域的网站想利用ajax交互数据 客户端:ajax的dataType参数设置成jsonp,然后设置一个回调函数(jsonCallBack) 服务器端:返回callfunName([{a:& ...

  9. 地图和定位 、 iCloud

    1 FindMe应用 1.1 问题 MapKit框架可以用于创建现场交互的地图来显示用户想要设备显示的任何位置,包括用户的当前位置,甚至可以进行标记并查看地图上的标注信息.CoreLocation框架 ...

  10. Python开发库

    在我多年的 Python 编程经历以及在 Github 上的探索漫游过程中,我发掘到一些很不错的 Python 开发包,这些包大大简化了开发过程,而本文就是为了向大家推荐这些开发包. 请注意我特别排除 ...