码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • lc--655:输出二叉树


    655. 输出二叉树 - 力扣(LeetCode)https://leetcode.cn/problems/print-binary-tree/

     

    分析:(列数和行数都已经告知了,只需要求出height即可。) 

    1. class Solution
    2. {
    3. public:
    4. int calDepth(TreeNode* root)
    5. {
    6. int h = 0;
    7. if (root->left)
    8. {
    9. h = max(h, calDepth(root->left) + 1);
    10. }
    11. if (root->right)
    12. {
    13. h = max(h, calDepth(root->right) + 1);
    14. }
    15. return h;
    16. }
    17. void dfs(vector>& res, TreeNode* root, int r, int c, const int& height) {
    18. res[r][c] = to_string(root->val);
    19. if (root->left)
    20. {
    21. dfs(res, root->left, r + 1, c - (1 << (height - r - 1)), height);
    22. }
    23. if (root->right)
    24. {
    25. dfs(res, root->right, r + 1, c + (1 << (height - r - 1)), height);
    26. }
    27. }
    28. vector> printTree(TreeNode* root)
    29. {
    30. int height = calDepth(root);
    31. int m = height + 1;
    32. int n = (1 << (height + 1)) - 1;
    33. vector> res(m, vector(n, ""));
    34. dfs(res, root, 0, (n - 1) / 2, height);
    35. return res;
    36. }
    37. };

     

    1. class Solution
    2. {
    3. public:
    4. int calDepth(TreeNode* root)
    5. {
    6. int res = -1;
    7. queue q;
    8. q.push(root);
    9. while (!q.empty())
    10. {
    11. int len = q.size();
    12. res++;
    13. while (len)
    14. {
    15. len--;
    16. auto t = q.front();
    17. q.pop();
    18. if (t->left)
    19. {
    20. q.push(t->left);
    21. }
    22. if (t->right)
    23. {
    24. q.push(t->right);
    25. }
    26. }
    27. }
    28. return res;
    29. }
    30. vector> printTree(TreeNode* root)
    31. {
    32. int height = calDepth(root);
    33. int m = height + 1;
    34. int n = (1 << (height + 1)) - 1;
    35. vector> res(m, vector(n, ""));
    36. queueint, int>> q;
    37. q.push({root, 0, (n - 1) / 2});
    38. while (!q.empty())
    39. {
    40. auto t = q.front();
    41. q.pop();
    42. int r = get<1>(t), c = get<2>(t);
    43. res[r][c] = to_string(get<0>(t)->val);
    44. if (get<0>(t)->left)
    45. {
    46. q.push({get<0>(t)->left, r + 1, c - (1 << (height - r - 1))});
    47. }
    48. if (get<0>(t)->right)
    49. {
    50. q.push({get<0>(t)->right, r + 1, c + (1 << (height - r - 1))});
    51. }
    52. }
    53. return res;
    54. }
    55. };

    待手撕(。。。。)

  • 相关阅读:
    计算机毕业设计ssm图书馆管理系统z3z90系统+程序+源码+lw+远程部署
    【Spring系列】- Bean生命周期底层原理
    Raft 协议
    【算法】【递归与动态规划模块】跳跃游戏
    Linux操作系统命令
    pyqt教程
    【Python爬虫原理与基本请求库urllib详解】
    Go内存管理逃逸分析
    基于Springboot实现网上商城管理系统演示【项目源码+论文说明】
    【计算机毕设之基于python的股票价格智能预测可视化系统-哔哩哔哩】 https://b23.tv/Rlgmbas
  • 原文地址:https://blog.csdn.net/zjjaibc/article/details/126460261
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号