LeetCode(1) Symmetric Tree
从简单的道题目開始刷题目:
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的更多相关文章
- LeetCode(25)-symmetric tree
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- LeetCode(101)Symmetric Tree
题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode(100) Same Tree
题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- LeetCode(94)Binary Tree Inorder Traversal
题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def hel ...
随机推荐
- [Apple开发者帐户帮助]六、配置应用服务(4)创建MusicKit标识符和私钥
要与Apple Music服务进行通信,您将使用MusicKit私钥对一个或多个开发人员令牌进行签名. 首先注册音乐标识符以识别您的应用.为使用Apple Music API的每个应用注册音乐标识符. ...
- selenium3 + python - xpath定位
什么是xpath呢? 官方介绍:XPath即为XML路径语言,它是一种用来确定XML1(标准通用标记语言3的子集)文档中某部分位置的语言.反正小编看这个介绍是云里雾里的,通俗一点讲就是通过元素的路径来 ...
- Python 35 线程(2)线程特性、守护线程、线程互斥锁
一:线程特性介绍 from threading import Thread import time n=100 def task(): global n n=0 if __name__ == '__m ...
- BZOJ 3831 单调队列DP
思路: 这好像是我刚学单调性的时候做的题 (我是不会告诉你 我被这题教做人了的...) i-stk[head]>k 删队头 f[stk[tail]]>f[i]||(f[stk[tail]] ...
- js实现“级联菜单”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- RRDtool入门详解
---------------原创内容,转载请注明出处.<yaoyao0777@Gmail.com>------------ 一.概述 RRDtool(round-robin databa ...
- VMWare虚拟机移动
1. 背景: 虚拟机:VM3 原安装路径:C:\Users\Administrator\Documents\Virtual Machines 移动到目标路径:D:\Virtual Machines ...
- 推荐系统:MovivLens20M数据集解析
MovieLens 是历史最悠久的推荐系统.它由美国 Minnesota 大学计算机科学与工程学院的 GroupLens 项目组创办,是一个非商业性质的.以研究为目的的实验性站点.MovieLens ...
- rev
功能说明:反向输出文件内容. 字符串反转 文本反转
- Lost connection to MySQL server at 'reading authorization packet', system error: 0_Mysql
1.大多数时候设置"set global connect_timeout=60:"是可以解决问题的. 我们可以通过执行“SHOWSTATUS LIKE 'aborted%'”,可以 ...