判断一棵二叉树是否对称

/**
* 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) {
return isSymmetric(root,root);
} bool isSymmetric(TreeNode* pRoot1,TreeNode* pRoot2){
if (pRoot1 == NULL && pRoot2 == NULL)
return true;
if (pRoot1 == NULL || pRoot2 == NULL)
return false;
if (pRoot1->val != pRoot2->val)
return false;
return isSymmetric(pRoot1->left, pRoot2->right) && isSymmetric(pRoot2->left, pRoot1->right);
}
};
/**
* 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) {
return isSymmetric(root,root);
} bool isSymmetric(TreeNode* pRoot1,TreeNode* pRoot2){
if (pRoot1 == NULL && pRoot2 == NULL)
return true;
if (pRoot1 == NULL || pRoot2 == NULL)
return false;
if (pRoot1->val != pRoot2->val)
return false;
return isSymmetric(pRoot1->left, pRoot2->right) && isSymmetric(pRoot2->left, pRoot1->right);
}
};

【easy】101. Symmetric Tree的更多相关文章

  1. 【LeetCode】101. Symmetric Tree (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  2. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  3. 【LeetCode】101 - Symmetric Tree

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

  4. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【leetcode❤python】101. Symmetric Tree

    #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):#     def __init_ ...

  6. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  7. 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树

    按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...

  8. 【easy】100. Same Tree

    判断两棵二叉树是否相等 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...

  9. 【Leetcode】【Easy】Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

随机推荐

  1. visual studio 配置属性中增加自定义宏和宏值

    visual studio中有一些预先定义的宏,用于配置项目属性,如SolutionDir.我们也可以自定义类似的宏,从而在配置包含目录(include)或添加依赖项时简化配置项. 如何创建自己的宏呢 ...

  2. java程序高CPU,如何直接定位(linux系统下命令行操作)

    1.top命令找出 2.也可以使用 (1)ps -ef|grep java|grep -v grep (2)jps -l|grep  公司名 然后,记住PID是9529. 3.定位具体的线程或者代码: ...

  3. application.properties

    #MySQLspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://lo ...

  4. BUGKU Misc 普通的二维码

    下载的文件是一个bmp文件,在我的印象中bmp好像没有什么隐写技巧,有些慌张. 既然是二维码,那不妨先扫一下试一试 哈哈!就不告诉你flag在这里! 嗯,意料之中 1首先我把它放到了stegosolv ...

  5. 【CF1146】Forethought Future Cup - Elimination Round

    Forethought Future Cup - Elimination Round 窝也不知道这是个啥比赛QwQ A. Love "A" 给你一个串,你可以删去若干个元素,使得最 ...

  6. laravel 【error】MethodNotAllowedHttpException No message

    Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message 报错原因[原理]CSRF ...

  7. 蓝书例题之UVa 10253 Series-Parallel Networks

    挺有趣的一道题 首先转化模型,思路参考蓝书,可得出等同于求共n个叶子,且每个非叶结点至少有两个子结点的无标号树的个数的二倍,设个数为\(f[n]\) 考虑怎么求\(f[n]\),假设有一个\(n\)的 ...

  8. TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

    https://blog.csdn.net/lanchunhui/article/details/61712830 https://www.cnblogs.com/silence-tommy/p/70 ...

  9. echarts纵坐标使用科学计数法表示

    最近做项目使用echart画图,发现纵坐标的刻度太大或太小的情况,导致页面十分难看,甚至出现遮挡的情况,所以想办法用科学计数法表示 代码如下: var option = { title: Echart ...

  10. CentOS安装glibc-2.14

    CentOS安装glibc-2.14   到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.gz wget https://ftp.gnu.org/gnu/ ...