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 ...
随机推荐
- c/c++ 数组 数组的引用,指针数组的引用
c/c++ 数组 知识点 1,数组的声明和初始化,对应代码里的test1和test2 2,char数组,对应代码里的test3 3,数组不可以拷贝和复制,对应代码里的test4 4,指针数组, 数组的 ...
- Cs231n课堂内容记录-Lecture 3 最优化
Lecture 4 最优化 课程内容记录: (上)https://zhuanlan.zhihu.com/p/21360434?refer=intelligentunit (下)https://zhua ...
- td 元素属性 noWrap 防止折行、撑开及文字换行
最近调试程序,遇到如下问题: 也就是这个表格里面的文字被换行了,究其原因,主要是td中的width之和超过了100%导致的.谷歌了好久,终于发现,可以用noWrap="noWrap" ...
- mysql 中的内置函数
一.字符串函数 select concat(name,"age is",age) from users; insert(str,x,y,insert)//将字符串x位置开始y个位 ...
- Ubuntu16.04系统安装搜狗输入法详细教程(转载)
1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...
- vue中父组件调用子组件函数
用法: 子组件上定义ref="refName", 父组件的方法中用 this.$refs.refName.method 去调用子组件方法 详解: 父组件里面调用子组件的函数,父组 ...
- JavaScript的对象详解
JavaScript对象的概述 什么是对象,代表现实中的某个事物, 是该事物在编程中的抽象,多个数据的集合体(封装体),用于保存多个数据的容器 为什么要用对象,便于对多个数据进行统一管理 对象属于一种 ...
- 1.01-url-open_code
import urllib.request def load_data(): url = "http://www.baidu.com/" #get的请求 #http请求 #resp ...
- 并发控制--Concurrency control--乐观、悲观及方法
In information technology and computer science, especially in the fields of computer programming, op ...
- NGINX+PHP+ZABBIX,推荐
https://www.cnblogs.com/liuzhennan/articles/5319280.html