Leetcode 101. Symmetric Tree(easy)
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 helper(TreeNode* left, TreeNode* right){
if (left == NULL && right == NULL){
return true;
}else if (left == NULL && right != NULL){
return false;
}else if (left != NULL && right == NULL){
return false;
}else if (left -> val == right -> val){
return helper(left -> left, right -> right) && helper(left -> right, right -> left);
}else{
return false;
}
}
bool isSymmetric(TreeNode* root) {
if (root == NULL){
return true;
}
return helper(root -> left, root -> right);
}
};
Leetcode 101. Symmetric Tree(easy)的更多相关文章
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 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 ...
- (二叉树 DFS 递归) 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 ----- java
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 e ...
- Java [Leetcode 101]Symmetric Tree
题目描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 回顾:Linux环境 Mysql新建用户和数据库并授权
回顾:Linux环境 Mysql新建用户和数据库并授权 一.新建用户 //登录Mysql @>mysql -u root -p @>密码 //创建用户 mysql> insert i ...
- [HBase_1] HBase安装与配置
0. 说明 1. 简介 1.1 简介 基于 HDFS 的大表软件(实时数据库) 十亿行 x 百万列 x 上千个版本 版本是通过 mvcc 技术控制:multiple version concurren ...
- Win10安装sqlserver2014打开显示黑色界面,mardown打开显示报错
问题描述: 我电脑从win7更新到win10以后就打开sqlserver2014显示黑色背景有问题,卸载了又装都是没有用 然后我又发现mardown也是有问题打开报告什么错误,忘记截图了,去网上找了个 ...
- xshell的一些常用配置
1 在xshell中鼠标滚轮和右键的快捷方式 利用鼠标滚轮和右键快速粘贴上面复制的内容 打开xshell的工具---选项-----键盘和鼠标 点击确定 回到xshell即可使用
- 《Java大学教程》—第15章 异常
自测题:1. 什么是异常?P357异常是在程序生命周期内出现的事件,它会导致程序行为不可靠. 2. 已检查异常和未检查异常的区别是什么?P359在编译器允许程序被编译通过前,要求程序员必须编写代 ...
- vue项目,ie11 浏览器报 Promise 未定义的错误
报错: {description: "“Promise”未定义", message: "“Promise”未定义", name: "Referenc ...
- MySQL高级知识(一)——基础
前言:MySQL高级知识主要来自尚硅谷中MySQL的视频资源.对于网上视频资源来说,尚硅谷是一个非常好的选择.通过对相应部分的学习,笔者可以说收益颇丰,非常感谢尚硅谷. 1.关于MySQL的一些文件 ...
- 邮票面值设计 (动态规划+DFS)
题意:https://ac.nowcoder.com/acm/problem/16813 思路: 深度搜索:每一层枚举一个面值,然后通过dp进行检查,并通过已知面值得到最多n张得到的最大表示数. 其实 ...
- 【转】用ffmpeg转多音轨的mkv文件
命令: ffmpeg -i AmericanCaptain.mkv -map 0:v -vcodec copy -map 0:a:1 -acodec copyAmericanCaptain.mp4 - ...
- Python:Day44 Javascript
一个完整的Javascript实现是由三个不同的部分组成: 1.核心 ECMA Javascript 2.浏览器对象模型(DOM) document object model (整合JS.html.C ...