【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 ...
随机推荐
- CTest
一.初识CTest CTest是CMake集成的一个测试工具,在使用CMakeLists.txt文件编译工程的时候,CTest会自动configure.build.test和展现测试结果 CTest有 ...
- 【PHP】金额数字转换成大写形式
<?php /*将数字金额转成大写*/ function num_to_upper($num) { $d = array('零','壹','贰','叁','肆','伍','陆','柒','捌', ...
- NSDateFormatter中时间格式串的含义
a: AM/PM (上午/下午) A: 0~86399999 (一天的第A微秒) c/cc: 1~7 (一周的第一天, 周天为1) ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat ( ...
- AddToDate
AddToDate is a PeopleCode built-in function for manipulating a date in PeopleCode. You can use it to ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- PHP生成二维码库phpqrcode
Description PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. ...
- 对"使用Mono Runtime Bundle制作安装包让C#桌面应用程序脱离net framework"增加说明
http://www.cnblogs.com/basilwang/archive/2011/11/29/2267809.html 想做独立引用的估计都看过这一篇文章,但是因为软件更新,很多地方已经不适 ...
- javascripy的innerHTML在IE8下的异常
使用jQuery的datatable插件的时候发现,IE8下显示异常,仔细调查一番,发现是浏览器对innerHTML的差异导致的. 实例代码: var nTd = document.createEle ...
- 如何快速重置OUTLOOK2013,2016到初始配置状态,outlook 修改数据文件位置
适用范围: 安装OUTLOOK的机器 知识点分析: 快速清除当前OUTLOOK所有账户,回归到初始配置状态. 操作步骤: WIN+R调出运行 输入: C:\Program Files (x86)\Mi ...
- IOS内存管理「2」- 点语法的内存管理