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. nginx https使用

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  2. Android - 定时服务 - Timer

    注:在项目中,有时可能会有一些定时执行的任务,这时,一般都会在一个service中写一个定时器. 例: Service类: import java.util.Timer; import java.ut ...

  3. OC中intValue要注意的地方

    在程序中,发现一个问题,写了个例子,如下:         NSDictionary * dict = [[NSDictionary alloc] init];        NSString * s ...

  4. Android中的五大布局

    Android中的五大布局 1.了解布局 一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件都有条不紊地 摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现了.布局是一种可用于放置很 ...

  5. 在MAC OS X上如何启用crontab?

    project: blog target: how-to-enable-crontab-on-osx.md date: 2015-12-16 status: publish tags: - OS X ...

  6. bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能

    xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很 ...

  7. 【winform 学习】登录

    一直都是做asp.net,没有做过winform项目,新建个项目后,就啥不会了,不知道从何下手. 简单的登录项目也不会,画了个登录界面后,就遇到了,跳入主界面后,怎样将登录界面关闭的问题. 在网上找到 ...

  8. JS运动基础(二) 摩擦运动、缓冲运动

    摩擦运动: 逐渐变慢,最后停止 缓冲运动: 与摩擦力的区别:可以精确的停到指定目标点距离越远速度越大速度由距离决定速度=(目标值-当前值)/缩放系数Bug:速度取整值取整: iSpeed = iSpe ...

  9. [专题汇总]AC自动机

    1.The 2011 ACM-ICPC Asia Dalian Regional Contest ZOJ 3545 Rescue the Rabbit  简单的AC自动机+状压DP, 状态DP[nod ...

  10. TCP三次握手及四次挥手详细图解

    TCP三次握手及四次挥手详细图解 Andrew Huangbluedrum@163.com    相对于SOCKET开发者,TCP创建过程和链接折除过程是由TCP/IP协议栈自动创建的.因此开发者并不 ...