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.
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root ==null) return true;
return isSy(root.left,root.right);
}
public boolean isSy(TreeNode s,TreeNode t) {
if(s == null || t == null) return s==t;
if(s.val != t.val) return false;
return isSy(s.left,t.right) && isSy(s.right,t.left);
}
}
101. Symmetric Tree(判断二叉树是否对称)的更多相关文章
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 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 ...
- 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3但是 ...
- LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 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 OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
随机推荐
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- python中的函数的执行分类
author:headsen chen date: 2018-03-21 17:42:13 notice:This article created by headsen chen himself ...
- if...else if...else和switch语句的注意点
java if...else if...else 语句 if 语句后面可以跟 elseif…else 语句,这种语句可以检测到多种可能的情况. 使用 if,else if,else 语句的时候,需要注 ...
- C 语言开发初涉-01 简单学习
尝试用C语言写windows程序: 简单的计算器 1.0版,仅用来熟悉C 开发windows的一些语法和语句用法 #include "stdafx.h" #include < ...
- 170406、用uid分库,uname(用户名)上的查询怎么办
[缘起] 用户中心是几乎每一个公司必备的基础服务,用户注册.登录.信息查询与修改都离不开用户中心. 当数据量越来越大时,需要多用户中心进行水平切分.最常见的水平切分方式,按照uid取模分库: 通过ui ...
- 160418、ztree权限菜单
1.页面中引用ztree的css和js(大家自行下载ztree) <link rel="stylesheet" href="${ctx}/resources/js/ ...
- EntityFramework.DynamicFilters 实现软删除和租户过滤
EntityFramework.DynamicFilters 实现软删除和租户过滤
- vue监听浏览器窗口大小变化
首先,页面初始化mounted的时候,通过 document.body.clientWidth 和 document.body.clientHeight 来获取到浏览器的宽和高,然后通过 window ...
- 自定义HTTP头时的注意事项(转)
原文:https://blog.gnuers.org/?p=462 HTTP头是可以包含英文字母([A-Za-z]).数字([0-9]).连接号(-)hyphens, 也可义是下划线(_).在使用ng ...
- LocalActivityManager与ActivityGroup
Helper class for managing multiple running embedded activities in the same process. This class is no ...