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. 利用ftp端口设置,浅谈windows防火墙之应用+ftp直接资源管理器登陆

    win服务器的版本是不错的.防火墙也比较能用.server-u 6.4则是比较稳定.兼容性好的的版本,所以很多人在用 1.服务器为了安全,一般开启windows高级防火墙,在网络连接处右键鼠标,有弹出 ...

  2. Python开发入门与实战10-事务

    1. 事务 本章我们将通过一个例子来简要的说明“事务”,这个开发实战里经常遇到的名词.事务是如何体现在一个具体的业务和系统的实现里. 事务是通过将一组相关操作组合为一个,要么全部成功要么全部失败的单元 ...

  3. springmvc 向页面传值

  4. UIlabel 属性text

    UILabel *pLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,200,100)]; pLabel.text = @"测试 ...

  5. having()方法设置查询条件,where()设置查询条件

    having  和 where区别 ① 使用有先后顺序 ② where  price>100     having price>100 ③ where  设置条件,字段必须是数据表中存在的 ...

  6. 关于if(a<b<c)判断的问题

    由于判断时的执行顺序,不要写成if(a<b<c)这种形式,很有可能得出的结果与我们想像的结果不一致,要写成if(a<b && b<c)!

  7. Android 优化List图片显示

    通常在界面中涉及到大量图片加载的时候都会产生卡顿,因此需要优化 其核心思想就是减少在getView()中的代码量和操作,让其尽可能的轻量化.众多方法最根本的目的是 将一切耗时的操作从getView中抽 ...

  8. java学习第十一天

    第十二次课 目标 一维数组(创建访问) 一.概念与特点 1.概念 相同数据类型的有序集合[] 数组名: 容器的名字 元素:  下标变量,数组名[下标] 长度:  length 下标:   位置.索引  ...

  9. pcl点云文件格式

    PCD版本 在点云库(PCL)1.0版本发布之前,PCD文件格式有不同的修订号.这些修订号用PCD_Vx来编号(例如,PCD_V5.PCD_V6.PCD_V7等等),代表PCD文件的0.x版本号.然而 ...

  10. Codeforces Round #161 (Div. 2)

    A. Beautiful Matrix 即相当于求1到中心位置\((2,2)\)的曼哈顿距离. B. Squares 排序,取倒数第\(k\)个即可. C. Circle of Numbers 固定\ ...