leetcode: Symmetric Tree
2015-08-27 17:46
204 查看
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
Note:
Bonus points if you could solve it both recursively and iteratively.
题目描述:
对称树的判断。
代码实现:
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.
题目描述:
对称树的判断。
代码实现:
/** * 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) { return root ? isSymmetric(root->left,root->right) : true; } bool isSymmetric(TreeNode *left,TreeNode *right) { if(!left && !right) return true;//终止条件 if(!left || !right) return false;//终止条件 return left->val== right->val && isSymmetric(left->left,right->right) && isSymmetric(left->right,right->left);//三方合并 } };
相关文章推荐
- N 的阶乘的另一种解法
- linux系统硬盘分区挂载及常用查看命令
- WPF中使用Image控件显示图片
- 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
- 分享MyEclipse安装及安装报错的解决方法
- LintCode-删除排序链表中的重复元素
- uploadPreview 上传图片预览
- Ajax实现跨域请求
- Storm-0.9.0.1安装部署 指导
- 初识Git
- Java利用Http 模拟POST、GET请求
- LoadRunner性能测试--90%响应时间
- LeetCode-Maximal Square
- textarea 在光标处插入文字
- python 倒计时 9,8,7,。。。。。。0
- system.out.println触发联想
- c++虚函数
- PHP高效文件缓存类FCache
- LoadRunner性能测试--%Disk Time 计数器值超过100%
- C# in depth (第九章 Lambda表达式和表达式树)