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:
But the following is not:
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.
分析
判断一棵二叉树是否为对称树;
仍然采用递归的思想,判断该树的左右子树是否对称;
若二叉树p与二叉树q对称,也就是说其根节点相同,p左子树应与q右子树对称,同理,p右子树应与q左子树对称;
AC代码
/**
* 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;
else
//判断左右子树是否对称
return isSymmetricTree(root->left, root->right);
}
bool isSymmetricTree(TreeNode* p, TreeNode* q) {
//如果两个二叉树均为空,则返回true
if (!p && !q)
{
return true;
}
//如果两者其一为空树,则返回false
else if (!p || !q)
{
return false;
}
else{
if (p->val != q->val)
return false;
else
//p左子树应与q右子树对称,同理,p右子树应与q左子树对称
return isSymmetricTree(p->left, q->right) && isSymmetricTree(p->right, q->left);
}
}
};
LeetCode(101)Symmetric Tree的更多相关文章
- LeetCode(1) Symmetric Tree
从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...
- LeetCode(25)-symmetric tree
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(103) Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- LeetCode(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
- LeetCode(26)-Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- LeetCode(101):对称二叉树
Easy! 题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2, ...
- LeetCode(102) Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- LeetCode(100) Same Tree
题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
随机推荐
- 害死人不偿命的(3n+1)猜想 (15)
#include <iostream> #include <algorithm> using namespace std; int main(){ int n; while ( ...
- Centos 6.x 搭建 Zabbix Agent 客户端
如需搭建zabbix server端,请参考:Zabbix-Server配置 环境: Zabbix-Server: Centos 6.8 IP:192.168.126.129 #Zabix- ...
- selenium中Xpath和CSS Selector的使用方法
一.selenium中Xpath的使用方法 1. 什么是xpath? Xpath是XML的路径语言,通俗一点讲就是通过元素的路径来查找这个标签元素 2. 练习Xpath的工具 火狐浏览器,下载插件Fi ...
- GCD Counting Codeforces - 990G
https://www.luogu.org/problemnew/show/CF990G 耶,又一道好题被我浪费掉了,不会做.. 显然可以反演,在这之前只需对于每个i,统计出有多少(x,y),满足x到 ...
- DOCTYPE的使用
定义和用法 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> 声明不是 HTML 标签:它是指示 we ...
- json和php数组 格式的互相转换
$json_arr = array('WebName'=>'PHP网站开发教程网','WebSite'=>'http://www.jb51.net'); $php_json = json ...
- Windows 2016 安装Sharepoint 2016 预装组件失败
Windows 2016 安装Sharepoint 2016 预装组件失败 日志如下: -- :: - Request for install time of Web 服务器(IIS)角色 -- :: ...
- 计算1至n的k次方的和
package com.ywx.count; import java.util.Scanner; /** * @author Vashon * date:20150410 * 题目:计算1至n的k次方 ...
- Angularjs 实现 $(document).ready()的两种方法
1.在controller里面利用$on或者$watch bookControllers.controller('bookctrl_test', ['$scope', '$routeParams', ...
- CSS 循环动画效果。
@-moz-keyframes revolving{ 0{ -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); } 25%{ ...