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. 【转】在网页中运行VB6程序

      用VB6做的程序在网页里运行, 需要把程序做成OCX格式,下面简单做一介绍: 首先新建一个工程, 选择ActivX控件: 然后添加控件和代码: 然后F5运行 然后按下图设置,去掉弹出消息阻止 这样 ...

  2. [SAP ABAP开发技术总结]初始值、空、NULL、INITIAL等问题

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

  3. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  4. 学习Berkeley DB- 入门

    1 导言 首先,我们要了解Berkeley DB的一些基本特性,在IBM的开发网站上有篇文章对其有比较清晰的介绍: 这篇文章讲到了BDB的设计思想和核心数据结构.以及数据访问算法:并有常用函数使用范例 ...

  5. 把多个JavaScript函数绑定到onload事件处理函数上

    为了让函数只在页面加载完毕后才得到执行,我们会把函数绑定到onload事件上: window.onload = userFunction 但如果有两个函数:firstFunction() 和 seco ...

  6. 详解.NET异步

    在说到异步前,先来理一下几个容易混淆的概念,并行.多线程.异步. 并行,一般指并行计算,是说同一时刻有多条指令同时被执行,这些指令可能执行于同一CPU的多核上,或者多个CPU上,或者多个物理主机甚至多 ...

  7. RAR暴破

    1. 网上稍微搜索了一下,貌似一个叫 "ARPR"的软件 出现的频率较高. 2. http://jingyan.baidu.com/article/a948d651b954a90a ...

  8. [转载] 一致性问题和Raft一致性算法

    原文: http://daizuozhuo.github.io/consensus-algorithm/ raft 协议确实比 paxos 协议好懂太多了. 一致性问题 一致性算法是用来解决一致性问题 ...

  9. mysql 查询开销

    1.select @@profiling;2.set @@session.profiling=on;3.show profiles;4.show profile for query 2;

  10. linux rwxrwxrwt文件夹属性

    /tmp 的permission是rwxrwxrwt chmod 0777 /abc       rwxrwxrwx chmod  777 /abc        rwxrwxrwx chmod 17 ...