leetcode-101. 判断对称树 · Tree + 递归
题面
判断给定二叉树是否对称。
Note : empty tree is valid.
算法
1. 根节点判空,若空,则返回true;(空树对称)
2. 根节点不空,递归判断左右子树。如果左右孩子都空,说明到了叶子,返回true;不都空而且一空一不空,返回false;都不空,且值不等,返回false,值相等,递归(左的左, 右的右) && (左的右, 右的左)
源码
/**
* 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) {
if(root == nullptr)
return true;
else
return Symmetric(root->left, root->right);
}
bool Symmetric(TreeNode* left, TreeNode* right)
{
if(left == nullptr && right == nullptr)
return true;
else if (left == nullptr || right == nullptr)
return false;
if(left->val == right->val)
return Symmetric(left->left, right->right) && Symmetric(left->right, right->left);
else
return false;
return true;
}
};
leetcode-101. 判断对称树 · Tree + 递归的更多相关文章
- [Leetcode 101]判断对称树 Symmetric Tree
[题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode Same Tree (判断相同树)
题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
随机推荐
- Greenwich.SR2版本的Spring Cloud Hystrix实例
之前我们在eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例)中,服务消费方a-beautiful-client调用服务提供方a-bootiful-clien ...
- 10--STL无序容器(Unordered Containers)
一:无序容器简介 Unordered Containers也是一种关联式容器.其中元素是分散,没有定性的排列(不是图中那样松散).其中元素可能在某一次操作后改变原来的位置. 哈希表的链地址法,更能表现 ...
- Mysql迁移由于字符集导致乱码的数据
有时候会在不注意的情况下创建了字符集为latin1的数据库,导致后续插入的中文显示乱码.这时有两种方法:1.修改数据库与数据表的字符集(只能向上调整,不能向下调整):2.数据迁移.但是两种方法都需要做 ...
- Block pool ID needed, but service not yet registered with NN java.lang.Exception: trace 异常解决
以上为报错信息: 原因大概为:dd和nd关联的versionId不同导致, 解决方案,备份之前的current文件夹,让其自己生成新的.
- Linux (Ubuntu)安装ssh
看ssh服务是否启动 打开"终端窗口",输入sudo ps -e |grep ssh 回车有sshd,说明ssh服务已经启动, 如果没有启动,输入sudo service ssh ...
- Casbin 使用记录
Casbin 是什么? 官方解释:Casbin是一个强大的.高效的开源访问控制框架,其权限管理机制支持多种访问控制模型. Casbin只负责访问控制.身份认证 authentication(即验证用户 ...
- fiddler https 抓包
手机上https抓包: http://blog.csdn.net/wangjun5159/article/details/52202059 需要给fiddler安装插件CertMaker for iO ...
- 【转】hr的嘴,骗人的鬼
入职前,从上往下读,入职后,从下往上读. - 我们非常欢迎新鲜血液补充进我们的团队:- 如果条件太苛刻,我待不久的:- 我们公司绝对不可能这样:- 请问每个月的工作都能按时足额发放吗?- 这难道不是理 ...
- ubuntu18.04 server配置静态ip,新的网络工具netplan的使用方法【转:http://forum.ubuntu.org.cn/viewtopic.php?t=487463】
最新发布的ubuntu18.04 server,启用了新的网络工具netplan,对于命令行配置网络参数跟之前的版本有比较大的差别,现在介绍如下:1.其网络配置文件是放在/etc/netplan/50 ...
- Jenkins加入systemctl管理
Jenkins安装目录为 /usr/local/jenkins-tomcat/ 添加文档 /usr/lib/systemd/system/jenkins.service [Unit] Descript ...