Leetcode57: Symmetric Tree
来源:互联网 发布:淘宝买家具 编辑:程序博客网 时间:2024/06/05 07:12
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
/** * 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 isMirror(TreeNode* left, TreeNode* right) { if(!left && !right) return true; if((!left && right) || (!right && left) || (left->val != right->val)) return false; return isMirror(left->left, right->right) && isMirror(left->right, right->left); } bool isSymmetric(TreeNode* root) { if(root == NULL) return true; if(!root->left && !root->right) return true; return isMirror(root->left, root->right); }};
0 0
- Leetcode57: 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
- 我的大二
- 网格与实心覆铜比较
- Code First Migrations更新数据库结构的具体步骤
- 多线程与异步的区别
- netfilter的原理
- Leetcode57: Symmetric Tree
- Mac上的抓包工具Charles
- 移动应用开发测试工具Bugtags集成和使用教程
- 25年前,开发者如何将游戏塞进内存?
- SQLserver平面数据导入异常
- Leetcode58: Balanced Binary Tree
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- JAVA设计模式之单例模式
- Ionic框架学习笔记(1)