【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 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.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
Solution 1: 递归,left对应right,left->left对应right->right,left->right对应right->left
/**
* 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 isSymmetric(TreeNode* root) {
if(!root)return true;
return isSymTree(root->left,root->right);
}
bool isSymTree(TreeNode *p,TreeNode *q){
if(!isSameNode(p,q))
return false;
else if(!p&&!q)
return true;
else
return isSymTree(p->left,q->right) && isSymTree(p->right,q->left);
}
bool isSameNode(TreeNode *p,TreeNode *q){
if(!p&&!q) //必需加上这个判断条件,否则若p、q为空下面的->val会运行时错误
return true;
else if((!p&&q)||(p&&!q)||(p->val!=q->val)) //利用||的短路效应避免运行时错误
return false;
return true;
}
};
Solution 2 :非递归,待续
【LeetCode】101 - Symmetric Tree的更多相关文章
- 【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】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【一天一道LeetCode】#101. Symmetric Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【easy】101. Symmetric Tree
判断一棵二叉树是否对称 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 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】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
随机推荐
- nodejs 文件上传
var multipart = require('connect-multiparty'); var fs = require('fs'); var multipartMiddleware = mul ...
- URL重写的优缺点分析
如何增强您网站地址的可读性.如何让搜索引擎快速收录到您的站点,这就需要优化您的Url,即Url的重写技术,大家熟悉的可能有很多服务器都提供Url重写技术,以前我们用的最多的就是Apache,Jboss ...
- 深入理解Java内存模型(一)——基础
并发编程模型的分类 在并发编程中,我们需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来交换信息.在命令式编程中,线程之间的通信 ...
- Volley HTTP库系列教程(1)简介及优点
Transmitting Network Data Using Volley Get started Dependencies and prerequisites Android 1.6 (API ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- Mysql中的Prepared Statement与Stored Precedure学习
可以参考: http://stackoverflow.com/questions/196652/prepared-statement-vs-stored-procedure They are not ...
- SVN 显示灰色减号代表什么意思
灰色减号(ignored):意思就是忽略的意思,不对其进行版本控制,忽略对其进行的任何操作
- CSS 中浮动的使用
float none 正常显示 left 左浮动 right 右浮动 clear none 允许两边浮动 left 不允许左边浮动 right 不允许右边浮动 both 不允许两边浮动 <!DO ...
- bzoj2396: 神奇的矩阵
与51nod1140一样.不过这题是多组数据的...坑.... #include<cstdio> #include<cstring> #include<cctype> ...
- 关闭oom killer
最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通常是因为某时刻应用程序大量请求内存导致系统 ...