判断一棵二叉树是否对称

/*** 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);}
};

转载于:https://www.cnblogs.com/sherry-yang/p/8445671.html

【easy】101. Symmetric Tree相关推荐

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

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

  2. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  3. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  4. 【LeetCode】【HOT】101. 对称二叉树(BFS+队列/递归)

    [LeetCode][HOT]101. 对称二叉树 文章目录 [LeetCode][HOT]101. 对称二叉树 package hot;class TreeNode{int val;TreeNode ...

  5. LeetCode 101. Symmetric Tree

    LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...

  6. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  7. 101. Symmetric Tree (C语言)

    101. Symmetric Tree (C语言) 判断是否为左右镜面对称的二叉树 题目 Given the root of a binary tree, check whether it is a ...

  8. 【数据结构】B-Tree, B+Tree, B*树介绍

    [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...

  9. Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

最新文章

  1. 用一个类根据Model属性生成SQL语句
  2. OpenGL之glMatrixMode函数的用法
  3. 使用jquery在新窗口中打开外部链接
  4. 用Python实现一个实时运动的大挂钟效果
  5. freemarker配置
  6. C#算法设计排序篇之06-堆排序(附带动画演示程序)
  7. 测试AtomicInteger与普通int值在多线程下的递增操作
  8. 如何快速解决腿抽筋?
  9. [状压dp][剪枝搜索] 洛谷 P2831 愤怒的小鸟
  10. 谈家装安防防盗设备的重要性
  11. reset.css下载
  12. 知乎热门:找工作时单位普遍要求 35 岁以下,那 35 岁以上的人都干嘛去了?
  13. 第一章--多媒体技术概述
  14. 安卓和苹果免填邀请码的功能怎么实现?
  15. 高级架构师_Elasticsearch_第二章kibana+IK分词器安装+索引操作+ 映射操作
  16. [Err] 1418 - This function has none of DETERMINIST
  17. 如何用 canvas 画出分形图
  18. 计算最大回撤python_python 计算收益回撤比
  19. asc量子计算机,2020ASC世界大学生超级计算机竞赛聚焦量子计算和语言智能
  20. 第23课:专项能力修炼

热门文章

  1. mysql的主从(AB)复制
  2. maven导出runnable jar
  3. vrrp 理论和配置
  4. OpenCV-python学习笔记(四)——smoothing and blurring平滑和模糊
  5. anaconda必须安装在c盘吗_Window版本 - Python数据分析集成开发环境 Anaconda安装(超详细)...
  6. LVS——TUN隧道模式(负载均衡)
  7. 写入多个表_制作属于自己的教学工作表
  8. keil5调试如何选择晶振_有源晶振的负载电容重要吗?
  9. 萌新的Python练习菜鸟100例(十五)利用条件运算符的嵌套来完成此题:学习成绩=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
  10. Delphi实现带有格式的Excel导出功能