判断一棵树是否自对称

可以回忆我们做过的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. Xcode升级插件失效,与添加插件不小心点击Skip Bundle解决办法

    一.当发现升级xcode后,插件不能使用,解决办法如下: 1.查看Xcode的UUID 在终端执行 defaults read /Applications/Xcode.app/Contents/Inf ...

  2. andorid SQLite数据库创建文件

    package com.hanqi.application3; import android.content.ContentValues; import android.database.sqlite ...

  3. iOS键盘出现时界面跟着往上推

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UI ...

  4. unreal3之FName及潜在bug

    FName是unreal3里对字符串高效处理的一种机制 基本原理就是把字符串hash存起来,然后每个字符串就只需要用一个数组索引值来表示了 FName的属性: NAME_INDEX Index; IN ...

  5. ycsb-命令及参数-与生成的负载类型相关

    loadbin/ycsb load mydb -P workloads/workloada -p "mydb.host=127.0.0.1" -p "mydb.port= ...

  6. PHP 判断数组是否为空的5大方法

    1. isset功能:判断变量是否被初始化 说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过 注意:当使用isset来判断数组元素是否被初始化过时,它的效率比array_key_ ...

  7. Android性能优化方法(六)

    ContentProvider优化改进 1.索引简单的说,索引就像书本的目录,目录可以快速找到所在页数,数据库中索引可以帮助快速找到数据,而不用全表扫描,合适的索引可以大大提高数据库查询的效率.(1) ...

  8. 嵌入式系统coredump设计

    阴沟翻船,马失前蹄,说明凡事皆有可能.自然,程序设计的再好,也会有crash的时候.开发期还还说,正式交付的系统crash自然更是难以承受的.无论何时,死一次就够了,得有方法查个水落石出. 几年前哥去 ...

  9. Model层数据验证

    问题1:View层如何向Controller的Action传递Model数据?在View中,可以使用Form表单进行模型数据的提交,同样的,我们需要关联提交数据的类型,则需要在View中使用@mode ...

  10. C2第六次作业解题报告

    看过题解后如果觉得还算有用,请帮忙加点我所在团队博客访问量 http://www.cnblogs.com/newbe/ http://www.cnblogs.com/newbe/p/4069834.h ...