判断一棵二叉树是否对称

/**
* 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. springboot中访问jsp文件方式

    首先,添加加载jsp文件的依赖包: <!--jsp依赖 对应springboot版本为2.1.4--><dependency> <groupId>org.apach ...

  2. Linux下ansible的group模块

    一.概述 group 模块可以帮助我们管理远程主机上的组. 二.常用参数 name参数:必须参数,用于指定要操作的组名称. state参数:用于指定组的状态,两个值可选,present,absent, ...

  3. python中的技巧——杂记

    杂记 zip的用法 对于 a = [1,2,3] b = [3,2,1] 若要同时遍历 for x, y in zip(a, b): pass zip(a, b)=> [(1,2,3),(3,2 ...

  4. [Ynoi2019模拟赛]Yuno loves sqrt technology I

    题目描述 给你一个长为n的排列,m次询问,每次查询一个区间的逆序对数,强制在线. 题解 MD不卡了..TMD一点都卡不动. 强制在线的话也没啥好一点的方法,只能分块预处理了. 对于每个块,我们设lef ...

  5. P1451 求细胞数量

    题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.(1<=m,n<=100)? 输入输出格式 输 ...

  6. java 数字左补齐0

    NumberFormat nf = NumberFormat.getInstance();        //设置是否使用分组        nf.setGroupingUsed(false);    ...

  7. visual studio 中sstrcpy报错的问题

    项目->属性->C/C++->预处理器->预处理器定义->添加 _CRT_SECURE_NO_WARNINGS

  8. rs(0)与rs("字段名")的区别

    rs(0)与rs("字段名")的区别    注意:rs是RecordSet对象 前者是按sqlyu语句读出来的记录急的先后顺序命名的,         rs(0)就是你select ...

  9. vue全局变量的使用

    新建一个VUE文件,声明一个变量,并且把它export. 在main.js中引入,并声明. 在其他地方使用,直接this就可以了.

  10. Technocup 2019 - Elimination Round 1

    http://codeforces.com/contest/1030 B. Vasya and Cornfield 判断点是否在矩形内(包括边界) 把每条边转化为一个不等式 public static ...