LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
/**
* 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; TreeNode *left;
TreeNode *right; queue<TreeNode *> q1, q2;
q1.push(root->left); q2.push(root->right); while (!q1.empty() && !q2.empty())
{
left = q1.front(); q1.pop();
right = q2.front(); q2.pop(); if (NULL == left && NULL == right)
continue;
if (NULL == left || NULL == right)
return false;
if (left->val != right->val)
return false; q1.push(left->left);
q1.push(left->right);
q2.push(right->right);
q2.push(right->left);
} return true;
}
};
LeetCode 101. Symmetric Tree(镜像树)的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Java for LeetCode 101 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 提高python处理数据的效率方法
处理大数据的方法有很多,目前我知道就这么多,后面会持续更新: 一.将数据分批次读取 csv格式是常见的数据存储方式,对于我们普通人而言易于读写.此外,在pandas中有pd.read_csv()函数可 ...
- Clion中配置使用更大的栈(防止开大一点的数组异常)
cmake_minimum_required(VERSION 3.15) project(TestProject) ) set(CMAKE_CXX FLAGS -fexec-charset=GBK) ...
- 关于子类和父类中的this的用法
public class Demo { public static void main(String[] args) { Fu f = new Zi(); f.show(); } } class Fu ...
- No root/virtual joint specified in SRDF. Assuming fixed joint
在用MoveIt!配置文件时,加载urdf模型时,显示Success......但没有显示模型,终端显示错误如下: 增加虚拟关节就好.
- oVirt-postgresql
连接数据库 方法一: cd /opt/rh/rh-postgresql95/root/bin su postgres ./psql \c engine 执行sql语句即可 方法二: 用pgAdmin访 ...
- python2和python3共存方法
拿到安装包,安装python3 centos: sudo yum install python36 ubuntu: sudo add-apt-repository ppa:deadsnakes/ppa ...
- Content-Type与MIME
http://www.cnblogs.com/jsean/articles/1610265.html 首先,我们要了解浏览器是如何处理内容的.在浏览器中显示的内容有 HTML.有 XML.有 GIF. ...
- day006-python函数
一.定义函数 def 函数名(): 函数体代码 例: #定义函数 def email(): print('send email') #调用执行函数 email() 二.函数的返回值 def email ...
- Cronicle 基于nodejs 的分布式任务调度工具
Cronicle 是一款不多的分布式任务调度工具,基于nodejs 开发,同时包含看web UI,从官方介绍看到的UI还是 挺不错的,而且功能很丰富,值得试用下 参考资料 http://cronicl ...
- 新blog
www.nancheng58.xyz 欢迎来访 骗访客量 我之前的blog是在csdn上的 https://blog.csdn.net/sinat_34550050 这里算是个在csdn的镜像吧 不过 ...