101. Symmetric Tree (C语言)

判断是否为左右镜面对称的二叉树

题目

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

Input: root = [1,2,2,null,3,null,3]
Output: false

Constraints:
The number of nodes in the tree is in the range [1, 1000].
-100 <= Node.val <= 100

解答

运用的是递归求解

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/bool isContrary(struct TreeNode* p, struct TreeNode* q)
{if(p == NULL && q == NULL)return true;else if((p == NULL && q != NULL) || (p != NULL && q == NULL))return false;elseif(p->val != q->val)return false;return (isContrary(p->left, q->right) && isContrary(p->right, q->left));
}bool isSymmetric(struct TreeNode* root){if(root == NULL) return true;return isContrary(root->left, root->right);
}

总结

运用了和“判定两个二叉树是否相同”类似的思路,一定要把握好哪些特征是相同的。在这道题中转化为,判定根节点的左子树和右子树是否是完全相反的。
“判定两个二叉树是否相同”:link

101. Symmetric Tree (C语言)相关推荐

  1. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  2. LeetCode 101. Symmetric Tree

    LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...

  3. Leetcode: 101. Symmetric Tree

    题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...

  4. Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  5. leetcode python3 简单题101. Symmetric Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...

  6. 【easy】101. Symmetric Tree

    判断一棵二叉树是否对称 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* T ...

  7. leetcode 101 Symmetric Tree

    判定两棵树是否严格镜像对称. 解决: BFS bool isSymmetric(TreeNode* root) {if (root == NULL)return true;deque<TreeN ...

  8. LeetCode Algorithm 101. Symmetric Tree

    Title 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1/ \2 2/ \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...

  9. Symmetric Tree (101)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

最新文章

  1. 架构师说了:不想做背锅侠?生产问题要这样查
  2. LeetCode Algorithm 1534. 统计好三元组
  3. 博客园CodingLife模板样式优化
  4. 牛客20701 神秘钥匙
  5. 广义线性模型——逻辑回归(logistic regression)
  6. 矩形变弧度角_懒惰使人类进步:不想刷马桶,那就让马桶自己变干净丨种草机...
  7. IOS APP提交流程
  8. python鼠标点击脚本_python模拟鼠标点击脚本
  9. markman,让设计更有爱!
  10. html5脑图_使用HTML5技术绘制思维导图
  11. 【Windows10】远程桌面共享多个显示屏
  12. PC天翼云盘v6.3.4精简版
  13. 计算机专业答辩开场白,计算机专业论文答辩开场白范文
  14. 【NLP】文献翻译1——基于结构和词嵌入的文本相似性测量方法
  15. (亲测有效)在SecureCRT终端挂载Ubuntu时遇到的问题
  16. 购物平台全店商品爬虫案例
  17. 专科程序员“霸面”蚂蚁金服,4轮面试,竟拿下offer(Java方向)
  18. mac-使用屏幕共享功能
  19. mysql 行号_mysql的行号问题
  20. C# 批量重命名文件

热门文章

  1. 如何避免搜索引擎爬虫产生的流量过大以及搜索引擎设置优化
  2. [F2F] Java基础 (JDK特性/集合/IO/锁)
  3. PAT 1009 蜜蜂寻路
  4. (28)Verilog实现倍频【方法三】
  5. guava的String之Splitter
  6. (十一)java.io.File学习进阶及separator、pathSeparator分隔符简介
  7. 【论文阅读】Detecting concurrency memory corruption vulnerabilities
  8. python好学吗 老程序员-学习python,难道是为了当一名苦逼的程序员吗?
  9. 基于Spring+SpringMVC+MyBatis超市进销存管理系统
  10. 2022爱分析・数据库厂商全景报告 | 爱分析报告