leetcode-101. Symmetric Tree
2016-05-01 11:21
302 查看
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
思路:递归,不递归的话用stack,但是逻辑会复杂
For example, this binary tree is symmetric:
思路:递归,不递归的话用stack,但是逻辑会复杂
/** * 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 == NULL) { return true; } return getSymmetric(root->left,root->right); } bool getSymmetric(TreeNode* left,TreeNode* right) { if(left == NULL || right == NULL) { return left == right; } if(left->val != right->val) { return false; } return getSymmetric(left->left,right->right) && getSymmetric(left->right,right->left); } };
相关文章推荐
- 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
- 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
- maven项目添加websocket
- 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
- c++第四次上机实验—作业
- jquery的$.extend和$.fn.extend作用及区别
- 手拖XMPP遇到问题解决地址
- www.mentalis.org/
- MyEclipse中输入变量名自动出现String
- 伸缩自如的时光轴实现
- 算法复习之二叉树的遍历
- 深入理解Java内存模型——重排序
- 二叉树的顺序存储结构
- 堆栈
- HDU 1242 Rescue(图)
- linux shell 常用基本语法
- 【HUSTOJ】1004: 求商与余数
- 链栈
- MySql: AUTO_INCREMENT
- 20145233韩昊辰 第四次实验报告