判断一棵树是否自对称

可以回忆我们做过的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. 在ASP.NET中上传附件

    前台页面使用ASP控件:<asp:FileUpload ID="FileUpload" runat="server" Style="margin ...

  2. Tcp之异常

    Tcp异常 昨研发报异常,据CMCC说是我方服务器主动断开的,于是怀疑是超时设置过短,于是我抓包,由于我接触socket时日尚短,搞不清为什么三次握手成功之后我方服务器会立刻发送fin 今天本来做实验 ...

  3. MVC学习地址

    http://www.cnblogs.com/n-pei/tag/Asp.net%20MVC/

  4. bootstrap的日期插件datetimepicker有问题

    bootstrap的日期插件datetimepicker在chrome中会出现掉下来的现象,而且一直没找到原因,下载最新版的插件直接在各个浏览器中都会掉下来, 问题一直解决不了,转而换其他插件 htt ...

  5. python 小知识

    PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 使用下面的代码可以打印PYTHONPATH: print(os.sys.path) impr ...

  6. oracle 多级菜单查询 。start with connect by prior

    select * from S_dept where CODE in(select sd.code from s_dept sd start with sd.code='GDKB' connect b ...

  7. 20145321 Git的安装使用及今后学习规划

    20145321 Git的安装使用及今后学习规划 Git安装使用及解决遇到的问题 之前上传代码都没有按照老师的方法弄,当时看到git教程感觉很麻烦,于是都是写完之后再一个个 程序贴上去,而现在使用过后 ...

  8. css小技巧之去掉蓝色底块的方法

    -moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit浏览器*/ -ms-user-select: none; /*IE1 ...

  9. Action类为何要继承ActionSupport

    Action类为何要继承ActionSupport   理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.open ...

  10. 传值 属性 block 单例 协议

    界面传值 四种传值的方式 1.属性传值(从前往后) 步骤: 1.属性传值用于第一个界面向第二个界面传值 2.明确二者联系的桥梁,也就是触发跳转的地方 3.明确传输的值,类型是什么 4.在第二个视图控制 ...