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 ...
随机推荐
- Linux 7 关闭、禁用防火墙服务
1 查看防火墙状态 [root@lvxinghao ~]# systemctl status firewalld 2 查看开机是否启动防火墙服务 [root@lvxinghao ~]# systemc ...
- linux简单命令2---文件搜索命令
1:文件搜索命令:locate 文件名搜索速度快,缺点不能复杂的搜索.在数据库(/var/lib/mlocate)查找.它是一天一更新.可以强制更新数据库:updatedb 2:搜索命令的命令:whe ...
- Python3 中codecs进行文件的读取
简单的概念与说明 编码(动词):按照某种规则(这个规则称为:编码(名词))将"文本"转换为"字节流".而在python 3中则表示:unicode变成str 解 ...
- Day9作业:socket之FTP工具
代码传的太累,直接发个github的链接吧! https://github.com/ccorzorz/Socketserver_FTP 上两张图给抛砖引玉下吧: 后台管理: FTP程序,包括客户端和s ...
- (二)第一个Servlet
一.预备知识 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个 ...
- 【python比较两个列表list】 python2 cmp() python3 operator模块
https://www.yiibai.com/python/list_cmp.html 如果元素的类型相同,则执行比较并返回结果.如果元素是不同的类型,则检查它们是否是数字. 如果数字,必要时进行数字 ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- iOS技术面试07:第三方框架
1ios框架分为:cocoa Touch:UIKit.Foundation.附属框架 Media:OpenGL ES.EAGL.Quartz.Core Animation.Core Audio. ...
- SSRF——漏洞利用(二)
0x01 概述 上篇讲述了SSRF的一般用法,用http协议来进行内网探测,攻击内网redis,接下来讨论的是SSRF的拓展用法,通过,file,gopher,dict协议对SSRF漏洞进行利用. 0 ...
- 【C/C++开发】ffplay中的FrameQueue的自我理解
最近在研究ffplay,以下是本人今天在研究FrameQueue的时候整理的笔记,如有错误还请有心人指出来~ //这个队列是一个循环队列,windex是指其中的首元素,rindex是指其中的尾部元素. ...