leetcode 101 Symmetric Tree ----- java
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.
* 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;
if( root.left == null && root.right == null)
return true;
if( root.left == null || root.right == null)
return false; return getResult(root.left,root.right); } public boolean getResult(TreeNode left,TreeNode right){
if( left == null && right == null)
return true;
if( left == null || right == null)
return false;
if( left.val != right.val )
return false;
if( getResult(left.left,right.right) )
return getResult(left.right,right.left);
return false; } }
也可以使用队列来解决这个问题。
leetcode 101 Symmetric Tree ----- java的更多相关文章
- [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 ...
- Java for 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 (对称树)
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
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 ...
- Leetcode 101. Symmetric Tree(easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 【NOIP模拟题】【二分】【倍增】【链表】【树规】
3 计算几何3.1 题意描述花花对计算几何有着浓厚的兴趣.他经常对着平面直角坐标系发呆,思考一些有趣的问题.今天,他想到了一个十分有意思的题目:首先,花花会在x 轴正半轴和y 轴正半轴分别挑选n 个点 ...
- Android 监听ContentProvider的数据改变
今天介绍一下怎么监听ContentProvider的数据改变,主要的方法是:getContext().getContentResolver().notifyChange(uri,null),这行代码是 ...
- python tuple 操作
特点:不可改(与字符串一样.不允许删除和修改) 操作:1.print 可使用跟%的元祖 print( '%s is %d years old' % (name, age)) 2.像列表一样有索引 3 ...
- 更新安装xcode7插件
mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-inscurl -fsSL https://raw.github ...
- poj1159 dp最长公共子串
//Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp ...
- object_c函数多个返回值
- (void)readStr1:(NSString**)str1 andStr2:(NSString**)str2{ NSString *s1 = @"1"; NSS ...
- sql server和oracle的差异
.部分SQL语句差异 (1)SQL:select top 10 * from table ORA: select * from table where rownum<11(2)SQL:S ...
- Python学习路程day6
shelve 模块 shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve d = shelve.open ...
- 算法导论 第六章 思考题 6-3 d叉堆
d叉堆的实现相对于二叉堆变化不大,首先看它如何用数组表示. 考虑一个索引从1开始的数组,一个结点i最多可以有d个子结点,编号从id - (d - 2) 到 id + 1. 从而可以知道一个结点i的父结 ...
- 5、C#基础整理(for 语句经典习题--与 if 的嵌套)
1.for循环最基本运用:小球每次落地后再弹起是以前的4/5,求第5次弹起后的高度 ; i < ; i++) { high = high*/; } Console.WriteLine(" ...