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

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. selenium3 + python - xpath定位

    什么是xpath呢? 官方介绍:XPath即为XML路径语言,它是一种用来确定XML1(标准通用标记语言3的子集)文档中某部分位置的语言.反正小编看这个介绍是云里雾里的,通俗一点讲就是通过元素的路径来 ...

  2. ROS-TF-Time

    前言:如何在特定时间进行转换.让第二只乌龟去第一只乌龟在5秒前的地方. 参考自:http://wiki.ros.org/tf/Tutorials/Time%20travel%20with%20tf%2 ...

  3. POJ 2418 简单trie树

    Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 21845 Accepted: 8551 De ...

  4. hbase无法启动,The node /hbase is not in ZooKeeper

    问题详细描述如下: 2016-12-09 15:10:39,160 ERROR [org.apache.hadoop.hbase.client.ConnectionManager$HConnectio ...

  5. SQL连接其它服务器操作

    Exec sp_droplinkedsrvlogin ZYB,Null --删除映射(录与链接服务器上远程登录之间的映射) Exec sp_dropserver ZYB --删除远程服务器链接 EXE ...

  6. Struts/Hibernate/Spring源码下载

    Struts: https://olex.openlogic.com/packages/struts Hibernate: https://olex.openlogic.com/packages/hi ...

  7. Percona Xtrabackup对数据库进行部分备份

    Xtrabackup也可以实现部分备份,即只备份某个或某些指定的数据库或某数据库中的某个或某些表.但要使用此功能,必须启用innodb_file_per_table选项,即每张表保存为一个独立的文件. ...

  8. java中的位运算及移位运算

    为了方便对二进制位进行操作,Java给我们提供了以下四个二进制位操作符: &    按位与 |     按位或 ^    按位异或 ~    按位取反 Java中有三个移位运算符: 左移:&l ...

  9. php知识点(基本上文档都有,只为方便记忆)

    类型转换 (unset)转换为NULL (binary) 转换和 b 前缀转换支持为 PHP 5.2.1 新增   转换二进制 隐藏php后缀名 AddType application/x-httpd ...

  10. Why use Cache-Control header in request?

    本地缓存也是缓存代理的一部分. 请求时使用Cache-Control 表示缓存的使用策略. 请求头里的no-cache表示浏览器不想读缓存,并不是说没有缓存.一般在浏览器按ctrl+F5强制刷新时,请 ...