LeetCode OJ 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 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.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
判断一棵树是不是对称的,那么我们需要对比两个位置对称的节点,首先判断这两个节点的值是否相等,然后判断这两个节点的子树是否对称。这就是递归的思路,从根节点的左右子树开始,递归向下。代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSame(root.left, root.right);
} public boolean isSame(TreeNode root1, TreeNode root2){
if(root1==null && root2==null) return true;
if(root1!=null && root2!=null){
if(root1.val != root2.val) return false;
else{
boolean a = isSame(root1.left, root2.right);
boolean b = isSame(root1.right, root2.left);
if(a==true && b==true) return true;
else return false;
}
}
return false;
}
}
LeetCode OJ 101. Symmetric Tree的更多相关文章
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- [LeetCode&Python] Problem 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【leetcode❤python】101. Symmetric Tree
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ 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 Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- LeetCode(1) Symmetric Tree
从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...
随机推荐
- 博客word测试
博客word测试 博客word测试 from __future__ import division, print_functionDOCLINES = (__doc__ or '').split(&q ...
- 【第七篇】Volley之处理Gzip数据
一般对于API请求需带上GZip压缩,因为API返回数据大都是Json串之类字符串,GZip压缩后内容大小大幅降低. public class GZipRequest extends StringRe ...
- 辽宁OI2016夏令营模拟T1-dis
数值距离(dis.pas/c/cpp)题目大意我们可以对一个数 x 进行两种操作:1. 选择一个质数 y,将 x 变为 x*y2. 选择一个 x 的质因数 y,将 x 变为 x/y定义两个数 a,b ...
- Python Data Visualization Cookbook 2.2.2
import csv filename = 'ch02-data.csv' data = [] try: with open(filename) as f://用with语句将数据文件绑定到对象f r ...
- 计算机网络课程优秀备考PPT之第三章数据链路层(三)
为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...
- PAT 团体程序设计天梯赛-练习集 L1-002. 打印沙漏
本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两 ...
- HDU 1008 u Calculate e
Problem Description A simple mathematical formula for e is where n is allowed to go to infinity. Thi ...
- hdu_5858_Hard problem(数学题)
题目链接:hdu_5858_Hard problem 题意: 让你求阴影部分面积 题解: 推推公式就行 #include<stdio.h> #include<math.h> # ...
- 12.hibernate命名查询
1.创建如下javaweb项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 <?xml version="1.0" encoding=" ...
- Windows系统新建gitignore文件出现“必须键入文件名”错误的解决办法
今天打算把本地的项目用git推送到github上去,但是有的信息我又不想把它加入到版本控制系统中去,例如.classpath文件和.class文件等等,这个时候我就想到了使用.gitignore文件把 ...