【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 ...
随机推荐
- Jquery入门之---------基本事件------------
Javascript有一个非常重要的功能,就是事件驱动. 当页面完成加载后,用户通过鼠标或键盘触发页面中绑定事件的元素即可触发.Jquery为开发者更有效率的编写事件行为,封装了大量有益的事件方法供我 ...
- 判断文件夹下是否存在txt格式的文本文件
判断D盘下是否存在txt类型的文件 string p_Path="D:\\"; bool IsHaveTxt() { DirectoryInfo foldinfo = new Di ...
- encodeURIComponent编码后java后台的解码
解决方法一: JavaScript: window.self.location="searchbytext.action?searchtext="+encodeURICompone ...
- FireFox Prevent this page from creating addtional dialogs 火狐浏览器 设置 阻止此页面创建更多对话框
FireFox英文版本老弹出“Prevent this page from creating addtional dialogs”的确认框 FireFox english version alert ...
- delphi的几个特别关键字 object absolute
1.object关键字相当于C++中的struct, record定义个结构体只能定义数据,而object可以定义方法,默认都是public的. 代码示例如下: TTest = record na ...
- 安装使用rspec
一,安装ruby. 二,运行命令,安装rspec的gem包: gem install rspec 会看到如下的结果: Fetching: rspec-core-2.14.7.gem (100%) Fe ...
- 【Cocoa】 Initializing View Instances Created in Interface Builder
Initializing View Instances Created in Interface Builder View instances that are created in Interfac ...
- 内核同步机制 RCU
Evernote分享地址:http://www.evernote.com/shard/s133/sh/8807320d-f54d-4e90-a31b-e2a3d35509ee/7539dc3931b8 ...
- 如何让Advanced Installer卸载软件时保留一些文件
http://www.advancedinstaller.com/user-guide/qa-keep-file.html You need to modify some of the resourc ...
- Android 设计模式
简介 项目开发中发现问题.解决问题这个过程中会出现很多问题,比如重复出现.某个问题的遗留,这些问题的本质就是设计模式.今天记录设计模式的知识点. 内容 在java以及其他的面向对象设计模式中,类与类之 ...