Symmetric Tree [LeetCode]
Problem description: http://oj.leetcode.com/problems/symmetric-tree/
Basic idea: Both recursive and iterative solutions.
Iterative solution:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(root == NULL)
return true; vector<TreeNode*> layer;
layer.push_back(root);
while(true) {
//determine if the tree is symetric
for(int i = ; i < layer.size()/; i ++ ) {
//layer[i] compare layer[layer.size() - 1 - i]
int right_idx = layer.size() - - i;
if((layer[i]->left == NULL && layer[right_idx]->right == NULL ||
(layer[i]->left != NULL && layer[right_idx]->right != NULL &&
layer[i]->left->val == layer[right_idx]->right->val)) &&
(layer[i]->right == NULL && layer[right_idx]->left == NULL ||
(layer[i]->right != NULL && layer[right_idx]->left != NULL &&
layer[i]->right->val == layer[right_idx]->left->val)))
continue;
else
return false;
} if(layer.size() % != ) {
int middle = layer.size() / ;
if(layer[middle]->left == NULL && layer[middle]->right == NULL ||
(layer[middle]->left != NULL && layer[middle]->right != NULL &&
layer[middle]->left->val == layer[middle]->right->val)){
//do nothing
}else{
return false;
}
} //get node for next layer
vector<TreeNode*> new_layer;
for(auto node : layer) {
if(node->left != NULL)
new_layer.push_back(node->left);
if(node->right != NULL)
new_layer.push_back(node->right);
}
if(new_layer.size() == )
break; layer = new_layer;
} return true;
}
};
Recursive solution: easier to understand.
class Solution {
public:
bool isSubSymmetric(TreeNode * left, TreeNode * right) {
if(left == NULL && right == NULL)
return true;
else if(left != NULL && right == NULL || (right != NULL && left == NULL))
return false;
else if(left->val != right->val)
return false;
else
return isSubSymmetric(left->left, right->right) && isSubSymmetric(left->right, right->left);
}
bool isSymmetric(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(root == NULL)
return true; return isSubSymmetric(root->left, root->right);
}
};
Symmetric Tree [LeetCode]的更多相关文章
- Symmetric Tree——LeetCode
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Symmetric Tree leetcode java
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- [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】Symmetric Tree
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: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
随机推荐
- ubuntu下chromium 安装flash player
原文地址 :http://blog.sina.com.cn/s/blog_858820890102v63w.html 不记得从什么时候起,Chromium 不再支持 Netscape plugin A ...
- TCP服务器不回复SYN的问题
个人问题发生环境: 1.TCP服务器是虚拟机,IP地址是192.168.8.12. 2.TCP客户端是宿主机,IP地址是192.168.8.11. 3.从宿主机(192.168.8.11)上启动Soc ...
- Creating Dynamic LOV in Oracle D2k Forms
Dynamic Lov is a good idea for the form where too many Lov requirement is there with different recor ...
- [SAP ABAP开发技术总结]屏幕跳转
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Spark ML聚类分析之k-means||
今天更新了电脑上的spark环境,因为上次运行新的流水线的时候,有的一些包在1.6.1中并不支持 只需要更改系统中用户的环境变量即可 然后在eclipse中新建pydev工程,执行环境是python3 ...
- web发展总结
- 花神的数论题(数位dp)
规定sum[i] 为i里面含1的个数 ,求从1-N sum[i]的乘积. 数为64位内的,也就是sum[i]<=64的,这样可以dp求出1-N中含k个1的数有多少个,快速幂一下就可以了. 有个地 ...
- 无限极分类,传递一个子ID得到所有父集,用于在前台分层显示标题
方法: static public function getParents($data,$id){ $arr=array(); foreach ($data as $v) { if ($v['id'] ...
- caffe的data_reader.cpp分析一下干了点什么
首先说明:下面的内容不一定对 类body: 变量:LayerParameter param_ :它里面放的是:body传进来的layerparameter的参数: BlockingQueue<s ...
- Object Pascal 过程与函数
过程与函数 过程与函数是实现一定功能的语句块,是程序中的特定功能单元.可以在程序的其他地方被调用,也可以进行递归调用.过程与函数的区别在于过程没有返回值,而函数有返回值. 1.过程与函数的定义 过程与 ...