判断一棵树是否自对称

可以回忆我们做过的Leetcode 100 Same Tree 二叉树Leetcode 226 Invert Binary Tree 二叉树

先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树

而我的做法是改下Same Tree的函数,改动的是第27行

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: bool isSymmetric(TreeNode* root) {
if(!root) return true;
return isSameTree(root->left, root->right); }
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q) {
return true;
}
else if(!p||!q){
return false;
}
else{
if(p->val != q->val ) return false;
else return isSameTree(p->left, q->right) && isSameTree(p->right, q->left);
}
}
};

Leetcode 101 Symmetric Tree 二叉树的更多相关文章

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

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

  2. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...

  4. LeetCode 101. Symmetric Tree (对称树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

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

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. leetcode 101 Symmetric Tree ----- java

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  7. LeetCode 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. Java [Leetcode 101]Symmetric Tree

    题目描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  9. LeetCode 101. Symmetric Tree 判断对称树 C++

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. redis学习(二) Redis Hash

    Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿). redis ...

  2. Markdown常用基本语法

    现在是我在学习Markdown时做的笔记.学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式:第一种:通 ...

  3. 。net 添加或获取文件关联

    文件关联设置 2011-02-07 14:25:36|  分类: VB.net2008或2010 |  标签:文件关联  |举报|字号 订阅     原理:以后缀名为.txt为例 方式一: 1.在注册 ...

  4. 访问https链接方法

    <a id='___szfw_logo___' href='https://credit.szfw.org/CX20160808028375110138.html' target='_blank ...

  5. three.js 之旅一

    扯一段废话,当你遇到一个没人知道的问题时,你该怎么办?         问周围人,他们遇到这种情况怎么办.作为程序员,这种情况肯定时有发生,我们要学会寻找资源. Three.js的六个基本步骤 1.设 ...

  6. App lifecycle(UWP深入学习一)

    https://msdn.microsoft.com/en-us/library/windows/desktop/br211474.aspx Launching, resuming, and back ...

  7. PopupWindow

    以前对于提示类型UI用到了PopupWindow 通过构造函数或者setContentView(View contentView)可以设置其显示内容: 显示时showAtLocation(View p ...

  8. 【转】让Chrome化身成为摸鱼神器,利用Chorme运行布卡漫画以及其他安卓APK应用教程

    下周就是十一了,无论是学生党还是工作党,大家的大概都会有点心不在焉,为了让大家更好的心不在焉,更好的在十一前最后一周愉快的摸鱼,今天就写一个如何让Chrome(google浏览器)运行安卓APK应用的 ...

  9. 业务代码中(java class)中如何实现多线程,并且将子线程中的值随方法返回返回值

    转载自http://bbs.csdn.net/topics/390731832 问题: public static String getAddress(final InputStream inputS ...

  10. onSaveInstanceState & onRestoreInstanceState

    一.onSaveInstanceState Called to retrieve per-instance state from an activity before being killed so ...