题目:

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. 1
  2. / \
  3. 2 2
  4. / \ / \
  5. 3 4 4 3

But the following is not:

  1. 1
  2. / \
  3. 2 2
  4. \ \
  5. 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.

代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. bool isSymmetric(TreeNode* root) {
  13. if (!root) return true;
  14. stack<TreeNode *> staL, staR;
  15. staL.push(root->left);
  16. staR.push(root->right);
  17. while ( !staL.empty() && !staR.empty() )
  18. {
  19. TreeNode *tmpL = staL.top();
  20. staL.pop();
  21. TreeNode *tmpR = staR.top();
  22. staR.pop();
  23. if ( !tmpL && !tmpR ) continue;
  24. if ( !tmpL || !tmpR ) return false;
  25. if ( tmpL->val != tmpR->val ) return false;
  26. staL.push(tmpL->right);
  27. staL.push(tmpL->left);
  28. staR.push(tmpR->left);
  29. staR.push(tmpR->right);
  30. }
  31. return staL.empty() && staR.empty();
  32. }
  33. };

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. CTest

    一.初识CTest CTest是CMake集成的一个测试工具,在使用CMakeLists.txt文件编译工程的时候,CTest会自动configure.build.test和展现测试结果 CTest有 ...

  2. 【PHP】金额数字转换成大写形式

    <?php /*将数字金额转成大写*/ function num_to_upper($num) { $d = array('零','壹','贰','叁','肆','伍','陆','柒','捌', ...

  3. NSDateFormatter中时间格式串的含义

    a: AM/PM (上午/下午) A: 0~86399999 (一天的第A微秒) c/cc: 1~7 (一周的第一天, 周天为1) ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat ( ...

  4. AddToDate

    AddToDate is a PeopleCode built-in function for manipulating a date in PeopleCode. You can use it to ...

  5. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  6. PHP生成二维码库phpqrcode

    Description PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. ...

  7. 对"使用Mono Runtime Bundle制作安装包让C#桌面应用程序脱离net framework"增加说明

    http://www.cnblogs.com/basilwang/archive/2011/11/29/2267809.html 想做独立引用的估计都看过这一篇文章,但是因为软件更新,很多地方已经不适 ...

  8. javascripy的innerHTML在IE8下的异常

    使用jQuery的datatable插件的时候发现,IE8下显示异常,仔细调查一番,发现是浏览器对innerHTML的差异导致的. 实例代码: var nTd = document.createEle ...

  9. 如何快速重置OUTLOOK2013,2016到初始配置状态,outlook 修改数据文件位置

    适用范围: 安装OUTLOOK的机器 知识点分析: 快速清除当前OUTLOOK所有账户,回归到初始配置状态. 操作步骤: WIN+R调出运行 输入: C:\Program Files (x86)\Mi ...

  10. IOS内存管理「2」- 点语法的内存管理