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

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. [Apple开发者帐户帮助]四、管理密钥(2)获取密钥标识符

    创建JSON Web令牌(JWT)以与启用的服务进行通信时,需要密钥标识符. 获取密钥标识符 在“ 证书,标识符和配置文件”中,选择侧栏中“键”下的“全部”. 在右侧,选择私钥. 密钥标识符显示在密钥 ...

  2. Django day07 (二)单表操作

    单表操作 -mysql数据库:settings里配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME ...

  3. css3 文本模型

    我前期是一名前端开发者,经常会遇到关于文本模型的问题,很多地方我们会经常遇到这种问题.例如栏目的标题,在设计师给我们图的时候并不会考虑到标题的长度,所以我们自己开发的过程中自己注意这一点. 首先和大家 ...

  4. Asp.net跨域配置

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  5. Dijkstra TYVJ 1031热浪 Dijkstra测试数据

    测试用邻接表写得Dijkstra 代码写得很烂. 描述 德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品.Farmer Jo ...

  6. jquery对象与DOM对象的转化(简化版):

    1:DOM对象 var apple = document.getElementById('apple'); 2:jquery对象 var _apple = $('#apple'); 3:DOM对象=& ...

  7. Android中ImageView的属性

    资源使用 Android 中支持三种格式的位图文件:.png(首选), .jpg(可接受),.gif(不建议) 为什么首推 PNG 呢? 官网的描述如下: 注:在构建过程中,可通过 aapt 工具自动 ...

  8. 水仙花数------"水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。(for循环的嵌套)

    package com.zuoye.test;//打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,//其各位数字立方和等于该数本身.//例如: ...

  9. nvcc fatal : Unsupported gpu architecture 'compute_11'

    使用VS编译OpenCV编译源代码时候,对Cmake生成的工程文件编译,会出现 nvcc fatal : Unsupported gpu architecture 'compute_11'  问题.原 ...

  10. 闲着无聊时写的一个调用天气 API 的小 Demo

    分为两个部分--调用以及实现,并且由于不想折腾,直接使用了 Console 来调用. 通过firefox直接调用 Main 入口,调用以及输出 调用部分没什么好说的,主要是针对 dynamic 类型的 ...