判断一棵二叉树是否对称

/**
* 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. JS string 常用方法总结

    var str = "abc123def666"; // charAt() 方法返回字符串中指定位置的字符. // 参数:index // console.log(str.char ...

  2. CRM销售人员针对的客户的权重分配的思想

    巧妙使用权重http://www.ziawang.com/article/52/ Django 项目CRM总结 - Pythia丶陌乐 - 博客园https://www.cnblogs.com/sup ...

  3. jsonp原理详解

    1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面.动态网页.web服务.WCF,只要是跨域请求,一律不准. 2.不过我们又发现,Web页面上调用js文件时则不 ...

  4. [转帖]5G网速那么快,基站辐射会很大吗?

    5G网速那么快,基站辐射会很大吗? 鲜枣课堂 2019-04-20 21:19收藏55评论6社交通讯     题图来自东方IC,本文来自微信公众号:鲜枣课堂(ID:xzclasscom),作者:小枣君 ...

  5. [Cordova inAppBrowser 在App内打开浏览器]

    方案1: 使用Cordova插件 cordova-plugin-inappbrowser 1.  添加插件 cordova plugin add cordova-plugin-inappbrowser ...

  6. Oracle 12c 静默安装(脚本自动化)

    oracle 12C 自动化静默安装脚本 项目地址: github: https://github.com/spdir/oracle-single-install 下载安装脚本 wget https: ...

  7. ref、out参数

    ref和out都是表示按引用传递.与指针类似,直接指向同一内存. 按值传递参数的方法永远不可能改变方法外的变量,需要改变方法外的变量就必须按引用传递参数. 传递参数的方法,在C语言里,用指针.在C#里 ...

  8. 关于java环境变量配置出现javac命令无法运行的解决办法

    昨天一时兴起给电脑刷了机,想着给电脑装个Win10+Linux的双系统, 结果双系统没装好,所有的东西又得重新弄一遍 今天在配置java的时候又出问题了 java,java-version运行成功了, ...

  9. 如何设置Maven代理

    1.公司的网络走的是代理,那么如何设置maven下载jar包时也走代理呢. 根据百度出来的两篇文章 设置了一下,但是还是报错. Plugin org.apache.maven.plugins:mave ...

  10. 关于FastDBF库读写ArcGis dbf文件的小bug

    该库托管于GitHub,地址:https://github.com/SocialExplorer/FastDBF 贡献者应该都是老外,所以…… 1.解析文件头,字段名部分如果有中文命名字段会出错 在D ...