从简单的道题目開始刷题目:

Symmetric Tree

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

For
example, this binary tree is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

    1
/ \
2 2
\ \
3 3

题目分析:

第一道题目简单的题目,主要利用递归方法,保存左右两个结点。对于LeftNode结点和RightNode结点,推断LeftNode的左结点和RightNode的右结点和LeftNode的右结点和RightNode的左结点是否相等就可以,仅仅要有不相等就能够结束。跳出递归。

代码:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: bool check(TreeNode *leftNode,TreeNode *rightNode)
{
if(leftNode == NULL && rightNode == NULL)
return true;
if(leftNode == NULL ||rightNode ==NULL)
return false;
if(leftNode ->val != rightNode->val)
return false; return check(leftNode->left,rightNode->right) && check(leftNode->right,rightNode->left); } bool isSymmetric(TreeNode *root) { if(root == NULL)
return true;
return check(root->left,root->right);
}
};

LeetCode(1) Symmetric Tree的更多相关文章

  1. LeetCode(25)-symmetric tree

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

  2. LeetCode(101)Symmetric Tree

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

  3. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  4. LeetCode(103) Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  5. LeetCode(124) Binary Tree Maximum Path Sum

    题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...

  6. LeetCode(26)-Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  7. LeetCode(102) Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  8. LeetCode(100) Same Tree

    题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  9. LeetCode(94)Binary Tree Inorder Traversal

    题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def hel ...

随机推荐

  1. C/C++中的位运算符

    --------开始-------- 我自己都记不住这是第几次把这几个位运算符搞混了,刚好在刚用过来把这几个位运算符记下来,俗话说的好好记性不如个烂笔头. 运算符: 与           或    ...

  2. php 过滤敏感关键词

    php 过滤敏感关键词 function badwords($content){ $keywords=M("config")->where("name='badwo ...

  3. AFN上传多张图片

    AFN上传多张图片代码: AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager]; sessionManager.r ...

  4. matplotlib之pyplot 学习示例

    现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子.  1. 简单的绘图( ...

  5. 大白话理解promise对象

    Promise  代表了未来某个将要发生的事件(通常是一个异步操作)  Promise 是异步编程的解决方案,能够简化多层回调嵌套,代表了未来某个将要发生的事件.Promise是一个构造函数,本身有a ...

  6. OPPO R9sPlus MIFlash线刷TWRP Recovery ROOT详细教程

    教程转载来自 残芯此生不换  OPPO R9sPlus 目前最简单的刷Recovery root 方法,强烈推荐 新机想要刷第三方卡刷包的最简单过程是:           手机关机-->下载M ...

  7. SLAM: Orb_SLAM中的ORB特征

    原文链接:什么是ORB 关于Orb特征的获取:参考 最新版的OpenCV中新增加的ORB特征的使用 ORB是是ORiented Brief 的简称,对Brief的特定性质进行了改进. ORB的描述在下 ...

  8. C++的Android接口---配置NDK

    一. 在安卓工具网站下载ADT:http://tools.android-studio.org/index.php 参考链接:http://1527zhaobin.iteye.com/blog/186 ...

  9. Java_Reflect反射

    类是对象,类是java.lang.Class类的实例对象.There is a class named Class class Foo{} public class ClassDemo{ public ...

  10. PHP并发IO编程实践

    PHP相关扩展 Stream:PHP内核提供的socket封装 Sockets:对底层Socket API的封装 Libevent:对libevent库的封装 Event:基于Libevent更高级的 ...