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 ...
随机推荐
- js往标签下插入标签的方法
js="document.getElementsByClassName('CodeMirror-line')[0].innerHTML = 'xxxxxx'"dr.execute_ ...
- LeetCode_66. Plus One
66. Plus One Easy Given a non-empty array of digits representing a non-negative integer, plus one to ...
- Java使用Apache Commons Net的FtpClient进行下载时会宕掉的一种优化方法
在使用FtpClient进行下载测试的时候,会发现一个问题,就是我如果一直重复下载一批文件,那么经常会宕掉. 也就是说程序一直停在那里一动不动了. 每个人的情况都不一样,我的情况是因为我在本地之前就有 ...
- 【数据库开发】MySQL命令大全
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- 多线程调用有参数的方法---c# Thread 与 Task
C#实现多线程的方式:Task——任务 简介 .NET 4包含新名称空间System.Threading.Tasks,它 包含的类抽象出了线程功能. 在后台使用ThreadPool. 任务表示应完 ...
- ELK实战搭建+x-pack安全认证
阅读目录: ELK日志平台入门简介1.1 ELK原理拓扑图1.2 Elasticsearch安装配置1.3 Kibana安装配置1.4 Kibana汉化及时区修改1.5 Logst ...
- ant design pro v2 关于用户登录有多个权限的解决方法
ant design pro V2菜单栏显示流程, 用户输入用户名,密码,登录调用登录接口,校验后返回该用户的权限字段currentAuthority,然后通过调用setAuthority(curre ...
- [转载]ftp和http区别
本文围绕以下三个部分展开: 一.HTTP协议 二.FTP协议 三.HTTP与FTP的异同点 一.HTTP协议简介 1. 概念 HTTP: HyperText Transfer Protocal,超文本 ...
- 用css美化select框
先上代码: .selectData{ height: 0.42rem; position: absolute; right:.28rem; top:.30rem; //去边框 border: none ...
- PHP基础之函数
函数概念: 函数是用来完成某种特定任务的可重用代码块; 函数可以使程序更具模块化,拥有良好的结构; 函数定义后在程序中可以重复调用; 函数分为内置函数和自定义函数 考点: 变量的作用域和静态变量 延伸 ...