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


    LeetCode-572. Subtree of Another 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/subtree-of-another-tree/

    Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.

    A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.

    Example 1:

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

    Example 2:

    Input: root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
    Output: false
    

    Constraints:

    • The number of nodes in the root tree is in the range [1, 2000].
    • The number of nodes in the subRoot tree is in the range [1, 1000].
    • -10^4 <= root.val <= 10^4
    • -10^4 <= subRoot.val <= 10^4

    【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. */
    12. class Solution {
    13. public:
    14. bool isSubtree(TreeNode* root, TreeNode* subRoot) {
    15. if (root == nullptr) { return false;}
    16. return isSubtreeWithRoot(root, subRoot)
    17. || isSubtree(root->left, subRoot)
    18. || isSubtree(root->right, subRoot);
    19. }
    20. bool isSubtreeWithRoot(TreeNode* pRoot1, TreeNode* pRoot2) {
    21. if (!pRoot1 && !pRoot2) {return true;}
    22. if (!pRoot1 || !pRoot2) {return false;}
    23. if (pRoot1->val != pRoot2->val) {return false;}
    24. return isSubtreeWithRoot(pRoot1->left, pRoot2->left)
    25. && isSubtreeWithRoot(pRoot1->right, pRoot2->right);
    26. }
    27. };

    【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 isSubtree(TreeNode root, TreeNode subRoot) {
    18. if (root == null) {return false;}
    19. return isSubtreeWithRoot(root, subRoot)
    20. || isSubtree(root.left, subRoot)
    21. || isSubtree(root.right, subRoot);
    22. }
    23. boolean isSubtreeWithRoot(TreeNode pRoot1, TreeNode pRoot2) {
    24. if (pRoot1 == null && pRoot2 == null) {return true;}
    25. if (pRoot1 == null || pRoot2 == null) {return false;}
    26. if (pRoot1.val != pRoot2.val) {return false;}
    27. return isSubtreeWithRoot(pRoot1.left, pRoot2.left)
    28. && isSubtreeWithRoot(pRoot1.right, pRoot2.right);
    29. }
    30. }

  • 相关阅读:
    业绩下滑、股价大跌,芯片厂商如何越过寒冬?
    android:mediaPlayer.setLooping(true);解决只循环一次
    LeetCode-剑指68-I.二叉搜索树的最近公共祖先
    协同过滤算法
    稳压器【TPS6283810YFPR 3A】汽车类、TPS629203QDRLRQ1,TPS74550PQWDRVRQ1采用小型6 引脚 WSON 封装。
    010.Springboot之养老院管理系统
    第五代验证码的“无感”进化史
    C++11 强枚举类型
    爬虫HTTP代理:获取多种类型数据的神器
    基于grpc从零开始搭建一个准生产分布式应用(6) - 03 - MapStruct高级映射
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126331459
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号