题目:

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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

代码:

/**
* 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;
stack<TreeNode *> staL, staR;
staL.push(root->left);
staR.push(root->right);
while ( !staL.empty() && !staR.empty() )
{
TreeNode *tmpL = staL.top();
staL.pop();
TreeNode *tmpR = staR.top();
staR.pop();
if ( !tmpL && !tmpR ) continue;
if ( !tmpL || !tmpR ) return false;
if ( tmpL->val != tmpR->val ) return false;
staL.push(tmpL->right);
staL.push(tmpL->left);
staR.push(tmpR->left);
staR.push(tmpR->right);
}
return staL.empty() && staR.empty();
}
};

tips:

深搜思想。设立两个栈:左栈和右栈。

从根节点开始:左子树用左栈遍历(node->left->right);右子树用右栈遍历(node->right->left)。

这样就可以转化为Same Tree这道题了(http://www.cnblogs.com/xbf9xbf/p/4505032.html

【Symmetric Tree】cpp的更多相关文章

  1. 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...

  2. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  3. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  4. 【Minimum Depth of Binary Tree】cpp

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  5. 【Convert Sorted List to Binary Search Tree】cpp

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  6. 【Convert Sorted Array to Binary Search Tree】cpp

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  7. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  8. 【Recover Binary Search Tree】cpp

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  9. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

随机推荐

  1. Knockout.Js官网学习(text绑定)

    前言 text 绑定到DOM元素上,使得该元素显示的文本值为你绑定的参数.该绑定在显示<span>或者<em>上非常有用,但是你可以用在任何元素上. 简单绑定 Today's ...

  2. Spring中Quartz调度器的使用

    一.Quartz的特点 * 按作业类的继承方式来分,主要有以下两种: 1.作业类继承org.springframework.scheduling.quartz.QuartzJobBean类的方式 2. ...

  3. PHP面向对象之旅:static变量与方法

    static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“类方法”. 如果访问控制权限允许,可不必创建该类对象而直接使用类名加两个冒号“ ...

  4. IntelliJ IDEA 13破解(JRebel 5.6.3a破解)

    首先安装IntelliJ 13,记得要下载Ultimate Edition版本,不然就不需要破解了.. 安装到本地,然后进行一些配置(这一步可以不要,但是考虑到以后换系统可以省事,推荐做) 打开{in ...

  5. Zend Studio下调试PHP的一点注意事项

    Zend Studio默认php文件的存放路径是你配置的服务器的路径,比如你配置的服务器是localhost,那么,你在zend下建立的文件均是相对于localhost而言的,比如你新建一个php工程 ...

  6. Noise,Error,wighted pocket Algorithm

    错误衡量(Error Measure) 有两种错误计算方法: 第一种叫0/1错误,只要[预测≠目标]则认为犯错,通常用于分类:通常选择,错误比较大的值作为y˜的值 第二种叫平方错误,它衡量[预测与目标 ...

  7. opensuse 安装 Anaconda3 之后出现Could not start d-bus. Can you call qdbus?

    最近在安装了opensue Leap42.1之后,想要学习一下python,就安装了Anaconda3,并且将Anaconda3的安装路径添加到了PATH里,但是在重新启动系统后,出现了"C ...

  8. java学习资源汇总

    http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

  9. C#学习笔记(与Java、C、C++和Python对比)

    (搬运自我在SegmentFault的博客) 最近准备学习一下Unity3D,在C#和JavaScript中选择了C#.所以,作为学习Unity3D的准备工作,首先需要学习一下C#.用了一两天的时间学 ...

  10. 刀哥多线程GCD核心概念gcd

    GCD GCD 核心概念 将任务添加到队列,并且指定执行任务的函数 任务使用 block 封装 任务的 block 没有参数也没有返回值 执行任务的函数 异步 dispatch_async 不用等待当 ...