【Symmetric Tree】cpp
题目:
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的更多相关文章
- 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- 【Maximum Depth of Binary Tree 】cpp
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 【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 ...
- 【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 ...
- 【Validate Binary Search Tree】cpp
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【Recover Binary Search Tree】cpp
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
随机推荐
- [系统]制作老毛桃U盘WinPE
准备工作,1G以上的U盘一个, Winpe 工具一个,个人推荐老毛桃winpe Ghost镜像文件(这个不用说了吧,能想到U盘装系统肯定都知道的) 可以开始了:插入U盘(最好把U盘里的东西清空,自己保 ...
- PHP总结
1.PHP的简介: PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器") 是一种通用开源脚本语言.语法吸收了C语言.Java和Per ...
- Windows2008下搭建NFS实现windows空间提供linux使用
我们既然是要把Windows 的硬盘给Linux使用 就需要在Windows上安装NFS网络文件系统 操作如下 1.添加角色-----文件服务---勾选网络文件服务 2.安装完成后 我们需要把这30 ...
- Sublime Text 快捷键
ctrl+shift+t:重新打开最近关闭文件 快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t ...
- Incorrect column name 'productid '
#1166 - Incorrect column name 'productid ' 解决方法:字段是复制的吧,复制的里面应该是有空格吧?检查一下,去掉就可以了哟,呵呵.
- 直接拿来用!超实用的Java数组技巧攻略[转]
来自csdn http://www.csdn.net/article/2013-09-16/2816947-methods-for-java-arrays 本文分享了关于Java数组最顶级的11大方法 ...
- BufferedReader,缓冲输入字符流
1. /** * 输入字符流: * --------|Reader 所有输入字符流的基类.抽象类 * -----------|FileReader 读取文件字符串的输入字符流. * --------- ...
- GOOGLE 离线完整安装包下载地址
https://support.google.com/chrome/answer/126299?hl=zh-Hans 官方链接介绍 https://www.google.com/chrome/brow ...
- 部署到iis后,发现无法加载运行CSS文件
解决方法: 打开或关闭window功能中的Internet信息服务里的万维网服务=>常见HTTP功能=>静态内容
- dump buffer cache
1.基础内容: ALTER SESSION SET EVENTS 'immediate trace name buffers level n'; n取值意义: 1 只转储buffer header. ...