题目:

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. shell脚本中切换用户并执行命令

    1.切换用户并执行命令 su 用户名 -c "命令" 2.切换用户并执行脚本 su 用户名 -s /bin/bash 脚本路径 3.切换用户并执行命令集su 用户名 << ...

  2. 转 网络IO模型:同步IO和异步IO,阻塞IO和非阻塞IO

    此文章为转载,如有侵权,请联系本人.转载出处,http://blog.chinaunix.net/uid-28458801-id-4464639.html 同步(synchronous) IO和异步( ...

  3. zencart安装第三步出现空白

    问题描述:在安装zencart时,在第三步同意协议,然后点击下一步时,页面经过漫长的等待后就显示空白. 解决方法:打开php.ini,将max_execution_time = 30 -> ma ...

  4. c#自定义进度条

    有些时候我们做的程序需要进度条,而vs提供的控件不是我们想要的.先看效果图:       进度条闪烁动画,当然背景可设为Transparent 之前想手绘进度条线条的,结果控件运行时会闪烁,所以直接用 ...

  5. MongoDB工具介绍

    在Windows下面,mongodb就只有一个bin目录以及bin目录以外的三个文件,相对bin目录中包括了如下文件: bsondump.exe 用于将导出的BSON文件格式转换为JSON格式 mon ...

  6. CSS中的content和attr的用法

    问题缘起 在闲看别人网站时发现了这样的代码 <span class="hamburger icon" data-icon="C"> .icon:be ...

  7. Yii 增删改查 测试记录

    亲们, 我是yii小白 不要笑话我奥.今天白天写一个管理模块涉及到 yii ar 下的  curd 操作,做 update 操作时纠结了好久,今天晚上花点时间学习, 下面写下我的测试记录 代码如下: ...

  8. pthreads多线程数据采集

    以前使用curl的多线程并不是真正的多线程,只是一种模拟的多线程,现在使用pthreads来实现真正意义上的多线程. 下载: windows下: http://windows.php.net/down ...

  9. webshell + xss 猥琐刷某投票

    团队成员发来一个投票的地址,需要撸某某网站的一个某某投票,果断看了下,ip限制了,看到post 数据包 额 随便找个大流量shell post 数据 Js代码代码 <script type=&q ...

  10. Material Design:CollapsingToolbarLayout

    activity_main.xml: <android.support.design.widget.CoordinatorLayout xmlns:android="http://sc ...