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]的更多相关文章

  1. Symmetric Tree——LeetCode

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. Symmetric Tree leetcode java

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  3. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  4. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  5. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  6. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  7. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  8. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  9. LeetCode: Symmetric Tree 解题报告

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

随机推荐

  1. 【转载】.NET程序员走向高端必读书单汇总

    原文:.NET程序员走向高端必读书单汇总 .NET程序员走向高端必读书单汇总 一.知识树 1. 基本能力 1.1 数学 1.2 英语 1.3 语言表达 2. 计算机组织与体系结构 3. 算法与数据结构 ...

  2. Spring的核心机制——依赖注入(Dependency Inject)

    Spring不仅提供对象,还提供对象的属性值,而不是由使用该对象的程序所提供的. Java应用是由一些相互协作的对象所组成的,在Spring中这种相互协作的关系就叫依赖关系. 如果A组件调用了B组件的 ...

  3. [SAP ABAP开发技术总结]OK_CODE

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. FJNU 1155 Fat Brother’s prediction(胖哥的预言)

    FJNU 1155 Fat Brother’s prediction(胖哥的预言) Time Limit: 1000MS   Memory Limit: 257792K [Description] [ ...

  5. Python基础学习笔记(九)常用数据类型转换函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...

  6. Python基础学习笔记(四)语句

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  7. LINQ之路 5:LINQ查询表达式

    书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询表达式(Query Expression). LINQ方法语法的本质是通过扩展方法和Lambda表达式来创建查询.C# ...

  8. JMS【三】--ActiveMQ简单的HelloWorld实例

    第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...

  9. JMS【二】--ActiveMQ简单介绍以及安装

    现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文JMS[一]--JMS基本概念,我们介绍了消息通信的规范JMS,我 ...

  10. [转载] linux 程序运行过程中替换文件

    今天被朋友问及“Linux下可以替换运行中的程序么?”,以前依稀记得Linux下是可以的(而Windows就不让),于是随口答道“OK”.结果朋友发来一个执行结果:(test正在运行中)# cp te ...