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 ...
随机推荐
- 原生JS---7
原生js学习笔记7——本地存储之cookie操作 什么是cookie • 用来保存页面信息的,如用户名.密码 • cookie的特性:同一个网站中所有的页面共享一套cookie:数量.大小限制:过期时 ...
- ios 指纹识别解锁
:添加LocalAuthentication.framework框架 :实现过程 #import "ViewController.h" #import <LocalAuthe ...
- Android Activity has leaked window that was originally added
今天调试程序时log中突然打印这样的错误,但是程序并没有crash,为了不放过一个错误,我决定调查一下. 当时是离开一个activity,然后提示是否退出此界面,接下来就打印此错误: - ::): A ...
- ThreadPoolExecutor理解
ThreadPoolExecutor组成 ThreadPoolExecutor的核心构造函数: public ThreadPoolExecutor(int corePoolSize, int maxi ...
- WEB笔记-CSS 实现多级导航效果
代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 团体程序设计天梯赛-练习集-L1-034. 点赞
L1-034. 点赞 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持 ...
- python tips:迭代器与可迭代对象
for循环 for i in s: print(i) 在上述for循环中,不断地将s中的值赋值给i,然后打印出来.这种只针对s中元素的循环称为对s的迭代,能够迭代的s称为可迭代的. python为了实 ...
- 单链表每k个节点为一组进行反转(最后不满k个时不反转)
public class LinkReverse2 { public static Node mhead=null; public static Node mtail=null; public sta ...
- eas之单据转换规则
/** * BOTP单据转换 * @param botpNum 转换规则编号 * @param BillInfo 原单据 */ public static void BOTP(String b ...
- 表操作(day03)
回顾: 1.单行函数 2.表连接 oracle中的表连接 内连接 等值连接 select e.id,e.first_name,d.name from s_emp e,s_dept d where e. ...