[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
【思路】
类似于100判断树是否相同,根节点开始分左右两路比较,三种情况讨论。p和q=null、p或q=null、p和q的val相同迭代。
不同在于mirror正好相反, 对left和right比较,即是fun(p1.left,p2.right)&&fun(p1.right,p2.left)。
【代码】
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return fun(root.left,root.right);
}
public boolean fun(TreeNode p1,TreeNode p2) {
if(p1==null&&p2==null)
return true;
if(p1==null||p2==null)
return false;
if(p1.val==p2.val)
return fun(p1.left,p2.right)&&fun(p1.right,p2.left);
return false;
}
}
[Leetcode 101]判断对称树 Symmetric Tree的更多相关文章
- 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] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- leetcode-101. 判断对称树 · Tree + 递归
题面 判断给定二叉树是否对称. Note : empty tree is valid. 算法 1. 根节点判空,若空,则返回true;(空树对称) 2. 根节点不空,递归判断左右子树.如果左右孩子都空 ...
- [Swift]LeetCode101. 对称二叉树 | Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【Leetcode】【Easy】Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...
- [Leetcode 100]判断二叉树相同 Same Tree
[题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...
- LeetCode Same Tree (判断相同树)
题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
随机推荐
- 第 7 章 多主机管理 - 045 - 安装 Docker Machine
安装 Docker Machine 先安装docker 官方安装docker-machine的文档地址:https://docs.docker.com/machine/install-machine/ ...
- hdoj5785
题意:略 先用题解的办法,manacher,然后tag,add数组.但是比较难办的是manacher加了新的字符.这样的话cntL和cntR不是实际的值,但是没关系,原本的字符都在奇数位置,这样cnt ...
- Spring Batch JSON 支持
Spring Batch 4.1 开始能够支持 JSON 格式了.这个发布介绍了一个新的数据读(item reader)能够读取一个 JSON 资源,这个资源按照下面的格式: [ { &q ...
- linux中的软、硬链接
linux中的软.硬链接 硬链接 硬链接(hard link),如果文件B是文件A的硬链接,则A的inode节点号与B的inode节点号相同,即一个inode节点对应两个不同的文件名,两个文件名指向同 ...
- poj2891 扩展中国剩余定理
求a1x1+r1=y...anxn+rn=y,crt合并 //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optim ...
- 1004. Max Consecutive Ones III最大连续1的个数 III
网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...
- [luogu P1438] 无聊的数列
[luogu P1438] 无聊的数列 题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个 ...
- Segment set(线段并查集)
Segment set Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total S ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
- pandas dataframe 过滤——apply最灵活!!!
按照某特定string字段长度过滤: import pandas as pd df = pd.read_csv('filex.csv') df['A'] = df['A'].astype('str') ...