码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-101. Symmetric Tree [C++][Java]


    LeetCode-101. Symmetric TreeLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/symmetric-tree/

    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

    Follow up: Could you solve it both recursively and iteratively?

    【C++】

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */

    递归

    1. class Solution {
    2. public:
    3. bool isSymmetric(TreeNode* root) {
    4. return root
    5. ? subtreeSymmetric(root->left,root->right)
    6. : true;
    7. }
    8. bool subtreeSymmetric(TreeNode* root1, TreeNode* root2){
    9. if (!root1 && !root2) {return true;}
    10. else if (root1 && root2 && (root1->val == root2->val)){
    11. return subtreeSymmetric(root1->left,root2->right)
    12. && subtreeSymmetric(root1->right,root2->left);
    13. } else {return false;}
    14. }
    15. };

    分步走,更清晰

    1. class Solution {
    2. public:
    3. bool isSymmetric(TreeNode* root) {
    4. return root
    5. ? subtreeSymmetric(root->left,root->right)
    6. : true;
    7. }
    8. bool subtreeSymmetric(TreeNode* root1, TreeNode* root2){
    9. if (!root1 && !root2) {
    10. return true;
    11. }
    12. if (!root1 || !root2) {
    13. return false;
    14. }
    15. if (root1->val != root2->val) {
    16. return false;
    17. }
    18. return subtreeSymmetric(root1->left,root2->right)
    19. && subtreeSymmetric(root1->right,root2->left);
    20. }
    21. };

    【Java】

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public boolean isSymmetric(TreeNode root) {
    18. return root != null
    19. ? subtreeSymmetric(root.left, root.right)
    20. : true;
    21. }
    22. boolean subtreeSymmetric(TreeNode root1, TreeNode root2){
    23. if (root1 == null && root2 == null) {return true;}
    24. else if (root1 != null
    25. && root2 != null
    26. && (root1.val == root2.val)) {
    27. return subtreeSymmetric(root1.left,root2.right)
    28. && subtreeSymmetric(root1.right,root2.left);
    29. } else {return false;}
    30. }
    31. }

    相似题目

    《剑指offer》28-- 对称的二叉树[C++]_贫道绝缘子的博客-CSDN博客NowCoder LeetCode题目描述请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), ...https://blog.csdn.net/qq_15711195/article/details/95995656

  • 相关阅读:
    html和winform webBrowser控件交互并播放视频(包含转码)
    万字剖析Ribbon核心组件以及运行原理
    今年最后一次PMP考试!想升职加薪抓紧!
    C#上位机与PLC
    手把手教学:二阶魔方还原
    如何做一份精致的性能测试报告?
    双字单字拆分/合并操作(博途SCL源代码)
    K8s集群环境搭建
    vSphere6.7创建Windows Server 2016虚拟机及磁盘扩容
    来看看老旧物件这样与现代空间结合完美结合
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126317542
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号