码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【打卡】牛客网:BM37 二叉搜索树的最近公共祖先


    自己写的:

    感觉写的很工整。

    1. /**
    2. * struct TreeNode {
    3. * int val;
    4. * struct TreeNode *left;
    5. * struct TreeNode *right;
    6. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    7. * };
    8. */
    9. class Solution {
    10. public:
    11. /**
    12. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    13. *
    14. *
    15. * @param root TreeNode类
    16. * @param p int整型
    17. * @param q int整型
    18. * @return int整型
    19. */
    20. int lowestCommonAncestor(TreeNode* root, int p, int q) {
    21. // write code here
    22. if(p > q){
    23. int temp = p;
    24. p = q;
    25. q = temp;
    26. }
    27. if(p <= root->val && q >= root->val) // p、q是不同节点
    28. return root->val;
    29. else if (q < root->val)
    30. return lowestCommonAncestor(root->left, p ,q);
    31. else
    32. return lowestCommonAncestor(root->right, p ,q);
    33. }
    34. };

    模板的:

    空间复杂度增加。用到了“记录搜索的路径”的方法,这可能是以后针对复杂问题也能用的、比较常用的模板吧。

    1. /**
    2. * struct TreeNode {
    3. * int val;
    4. * struct TreeNode *left;
    5. * struct TreeNode *right;
    6. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    7. * };
    8. */
    9. class Solution {
    10. public:
    11. /**
    12. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    13. *
    14. *
    15. * @param root TreeNode类
    16. * @param p int整型
    17. * @param q int整型
    18. * @return int整型
    19. */
    20. vector<int> getPath(TreeNode* root, int target){
    21. vector<int> path;
    22. while(root->val != target){
    23. path.push_back(root->val);
    24. if(root->val < target)
    25. root = root->right;
    26. else
    27. root = root->left;
    28. }
    29. path.push_back(root->val);
    30. return path;
    31. }
    32. int lowestCommonAncestor(TreeNode* root, int p, int q) {
    33. // write code here
    34. vector<int> path1 = getPath(root, p);
    35. vector<int> path2 = getPath(root, q);
    36. int res;
    37. for(int i = 0; i < path1.size() && i < path2.size(); i++){
    38. if(path1[i] == path2[i])
    39. res = path1[i];
    40. else
    41. break;
    42. }
    43. return res;
    44. }
    45. };

  • 相关阅读:
    【Spring Security】安全框架学习(八)
    AIGC(生成式AI)试用 7 -- 桌面小程序
    华为配置AP和AC之间NAT穿越示例
    【使用VS开发的第一个QT项目——实现相机功能(包括QT下载、配置、摄像头程序)】
    第四章 作业【数据库原理】
    IPQ6010+QCN9074|QCN9074-6E Throughput Test Report in DR6018
    基于PaddlePaddle框架对CIFAR-100数据集在简易CNN(LeNet-5修改)和简易DNN的效果对比
    洛谷 P1343 地震逃生(最大流dinic算法)
    SpringAOP执行流程——从源码画流程图
    Docker学习(5)—— 在Docker上安装软件
  • 原文地址:https://blog.csdn.net/weixin_47173826/article/details/134283700
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号