Symmetric Tree
来源:互联网 发布:会计记账软件 编辑:程序博客网 时间:2024/06/07 14:40
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 binary tree * 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 isSymmetric(root,root); }private: bool isSymmetric(TreeNode *p,TreeNode *q){ if(!p && !q) return true; if(p && q){ if(p->val==q->val){ bool lhs = isSymmetric(p->left,q->right); bool rhs = isSymmetric(p->right,q->left); return lhs && rhs; }else{ return false; } }else{ return false; } }};
0 0
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- 现实中如何评判路遥《人生》中的高加林?
- linux每日一学-netstat指令(3)
- 特种浓缩分离:新型有机管式超滤膜组件结构形式
- 工业水处理:威立雅工业水处理工程利用反渗透工艺
- 剑指offer系列源码-数组中的逆序对
- Symmetric Tree
- SVN脚本备份与恢复的方案
- Windows Phone 8 锁屏背景与通知
- 2014ACM/ICPC亚洲赛上海赛区总结
- dwr 3.0 配置 + 不同参数,不同返回值调用方法 示例
- jQuery实现拖拽
- 栈(1)----用链表实现栈
- [LeetCode] Best Time to Buy and Sell Stock II
- iOS疯狂详解之清理工程中没有用到的图片