[LeetCode101]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
Note:
Bonus points if you could solve it both recursively and iteratively.
分类:Tree BFS DFS
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: bool isSymmetric(TreeNode *root) {
if (!root) return true;
return helper(root->left, root->right);
} bool helper(TreeNode* p, TreeNode* q) {
if (!p && !q) {
return true;
} else if (!p || !q) {
return false;
} if (p->val != q->val) {
return false;
} return helper(p->left,q->right) && helper(p->right, q->left);
}
};
[LeetCode101]Symmetric Tree的更多相关文章
- 第28题:leetcode101:Symmetric Tree对称的二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 38. Same Tree && Symmetric Tree
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
随机推荐
- 《Java程序代理器》- java桌面程序运行的前端启动框架
虽说让java直接在桌面运行,有很多方法,但最简单的还是有个exe双击执行 要java执行就得有虚拟机,但原本的虚拟机文件体积太大,不方便随同打包,精简的虚拟机功能又不全,指不定什么时候报错 所以正规 ...
- acdream 1222 Quantization Problem [dp]
称号:acdream 1222 Quantization Problem 题意:给出一个序列 a ,然后给出一个 n * m 的矩阵,让你从这个矩阵中选出一个序列k,使得sum(abs(ki - ai ...
- poj3207(two-sat)
传送门:Ikki's Story IV - Panda's Trick 题意:给定一个圆,圆上一些点.两点一线.现给出一些线,这些线可以在圆内连起来,也可以在圆外.问有没有可能所有的线画完且不出现相交 ...
- jquery ui中 accordion的问题及我的解决方法
原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...
- 一步一步学android之事件篇——单选按钮监听事件
在平常使用软件的时候,我们经常会碰见一些选择题,例如选择性别的时候,在男和女之间选,前面说过这个情况要用RadioGroup组件,那么点击了之后我们该怎么获取到选择的那个值呢,这就是今天要说的OnCh ...
- SE 2014年4月16日
一. 描述BGP路由协议中 BGP路由携带 AS-PATH/ next-hop / ORIGIN / local-preference 属性的特点! BGP协议中的AS-PATH是AS列表,用来 ...
- hdu4027(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 线段树功能:区间修改,区间求和. 分析:因为每个数至多开6次平方就变1了,所以对于每一段全为1的 ...
- poj3264(线段树区间求最值)
题目连接:http://poj.org/problem?id=3264 题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差. ...
- MySQL的一些基本操作
近期開始学习MySQL,主要是通过书籍,和看燕十八老师的视频,然后通过博客记录自己的学习过程. 登入数据库 zhiniaobu@telunsu-K55VD:~$ mysql -uroot -p Ent ...
- 淘宝分布式 key/value 存储引擎Tair安装部署过程及Javaclient測试一例
文件夹 1. 简单介绍 2. 安装步骤及问题小记 3. 部署配置 4. Javaclient測试 5. 參考资料 声明 1. 以下的安装部署基于Linux系统环境:centos 6(64位),其他Li ...