判断一棵树是否自对称

可以回忆我们做过的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. java读取properties配置文件方法(一)

    为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...

  2. ue4 SNew背后的逻辑

    ue4的ui库Slate体系非常庞大,即使是在创建对象这一小事上,也是相当复杂: SLATECORE_API TSharedRef<SWidget> SNullWidget::NullWi ...

  3. 搭载hexo+github博客系统

    一.安装 Node.js 在官网 http://nodejs.org/ 下载winwos版本,点击安装完成即可. 二.git环境安装 对于git的安装,网上已经大量资料,就不赘述了. 三.安装 hex ...

  4. STF(SmartPhone Test Farm)Mac版本环境搭建

    它的github页面为: https://github.com/openstf/stf 1.Linux一些基本包的安装: 在控制台分别运行 sudo apt-get update sudo apt-g ...

  5. Android -- The Manifest File

    Before the Android system can start an app component, the system must know that the component exists ...

  6. lab 7 函数超级多的类

    #include<iostream>#include<string>#include<cmath>using namespace std; class Ration ...

  7. JS 插件使用

    1.时间控件的使用 My97DatePicker WdatePicker({ minDate: '%y-%M-{%d}'}) 默认当前日期以后 LoanMessage.LoanAppAgentMode ...

  8. centos 安装flash插件

    方法一: 1.选择合适的yum源http://get.adobe.com/cn/flashplayer/进入此网址选择 “YUM,适用于Linux(YUM)”,下载adobe源http://101.9 ...

  9. Microsoft Visual Studio 2010 VSTS单元测试指南

    本来以为很简单的一个问题,今天预计10分钟搞定,结果到下班还没弄出结果,单元测试运行的时候一直处于无反应状态,最后估计可能是我装的2010有问题,结果到家一试果然是有问题,有时软件就是这么神奇. 言归 ...

  10. cmd

    ExecuteNonQuery  返回影响的行数 ExecuteScalar       返回第一行第一列