5.1.7 Symmetric Tree
来源:互联网 发布:梨园淘宝城图片 编辑:程序博客网 时间:2024/06/14 11:25
ifficulty: Easy Source: http://leetcode.com/onlinejudge#question_101 Notes: 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. Solution: 1. Recursive solution 2.Iterative way (queue). */ /** * 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_2(root); } bool isSymmetric_1(TreeNode *root) { if (root == NULL) return true; return solve (root->left, root->right); } bool solve(TreeNode * t1, TreeNode * t2) { if (!t1 && !t2) return true; if (!t1 && t2 || t1 && !t2 || t1->val != t2->val) return false; return solve(t1->left, t2->right) && solve(t1->right, t2->left); }
};
0 0
- 5.1.7 Symmetric Tree
- 5.1.9 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
- getopt函数
- Gradle学习系列之五——自定义Property
- MFC拆分字符串
- 第十四周项目二:用文件保存的学生名单
- 浏览器client访问服务器socket(c++)
- 5.1.7 Symmetric Tree
- Gradle学习系列之六——使用java Plugin
- 2014年11月SAT考试真题答案
- iOS 合并.a文件,制作通用静态库
- Hadoop实现Clustering by fast search and find of density peaks
- 让query_posts() 或 wp_query()同时使用多个 meta_key与 meta_value 来查询多个自定义字段与值
- checkbox控件
- C#利用Renci.SshNet类库实现SFTP协议操作文件
- Gradle学习系列之七——依赖管理