判断一棵树是否自对称

可以回忆我们做过的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. JavaScript求最大数最小数

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  2. TransactionScope 事务使用说明

    TransactionScope是.Net Framework 2.0滞后,新增了一个名称空间.它的用途是为数据库访问提供了一个“轻量级”[区别于:SqlTransaction]的事物.使用之前必须添 ...

  3. 使用JMeter进行负载测试——终极指南

    这篇教程讨论的是JMeter,它是一款基于Java的.集合了几个应用程序.具有特定用途的负载和性能测试工具. 本篇主要涉及的内容: 解释一下JMeter的用途 JMeter的实现方式以及采用的技术 安 ...

  4. sam9x5 sam-ba

     调试参考 http://www.docin.com/p-872951702.html AT91SAM9x5 EK Board SoC Features Kit Information Kit Ove ...

  5. 怎样把windows中安装的程序列出来?

    症状/问题我怎样把windows中安装的程序信息输出到一个文本文件中?解决方法使用 windows 操作系统中的命令:wmic就可以做到.下面的命令就可以把系统中安装的程序都输出到文件ProgramL ...

  6. JDBC常用接口详解

    JDBC中常用接口详解 ***DriverManager 第一.注册驱动 第一种方式:DriverManager.registerDriver(new com.mysql.jdbc.Driver()) ...

  7. 浅谈Android 6.0之Runtime Permissions

    前言 Android6.0发布后,其一系列新特新足够让我们这些Android程序员兴奋一段时间了.首先我们先看看具体有哪些新特性: -锁频下语音搜索 -指纹识别 -更完整的应用权限管理 -Doze电量 ...

  8. Razor标记语言介绍

    什么是Razor?   Razor的中文意思是"剃刀",它不是编程语言,只是一种服务器段的标记语言,与PHP和ASP类似   Razor允许你向网页中嵌入基于服务器的代码(Visu ...

  9. 反射生成SQL语句

    public static int Reg(Model ml) { bool b = true; Visit vt = new Visit(); StringBuilder builder = new ...

  10. [转]Python程序员必须知道的30条编程技巧

    30 tips & tricks for Python Programming 1  直接交换两个数字位置 x, y = 10, 20 print(x, y) x, y = y, x prin ...