leetcode--- Symmetric Tree---平衡二叉树
2016-04-09 16:35
387 查看
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
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 ok(TreeNode* left, TreeNode* right) { if(left == NULL && right == NULL) return true; else if((left == NULL && right != NULL) || (left != NULL && right == NULL)) return false; else if(left->val != right->val) return false; return (ok(left->left, right->right) && ok(left->right, right->left)); } bool isSymmetric(TreeNode* root) { if(root == NULL) return true; TreeNode *left, *right; left = root->left; right = root->right; return ok(left, right); } };
相关文章推荐
- 初试牛客网PAT练习之数素数
- AndroidStudio配置NDK开发环境
- HTML5实现全屏API【进入和退出全屏】
- NYOJ92图像有用区域
- webstorm快捷键说明
- Python yield 使用浅析 (ibm-developerworks)
- hdu1021 Fibonacci Again
- 阅读下拉刷新控件代码有感
- java中的Stack和Queue之间用法解释,还有heap
- AlarmManager 定时广播,定时服务
- SQLServer复制需要有实际的服务器名称才能连接到服务器,请指定实际的服务器名
- poj 3469 Dual Core CPU(dinic算法)
- PYTHON之路(十)
- PHP算法——生成唯一字符串
- Java中整形和byte数组相互转换,支持正负数
- java 数组的运用
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- XML语法学习
- HTML5全屏(Fullscreen)API详细介绍_html5教程技巧
- SVN需求分析报告、序例图和流程图