码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-1110. Delete Nodes And Return Forest [C++][Java]


    LeetCode-1110. Delete Nodes And Return ForestLevel 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/delete-nodes-and-return-forest/

    Given the root of a binary tree, each node in the tree has a distinct value.

    After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).

    Return the roots of the trees in the remaining forest. You may return the result in any order.

    Example 1:

    Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
    Output: [[1,2,null,4],[6],[7]]
    

    Example 2:

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

    Constraints:

    • The number of nodes in the given tree is at most 1000.
    • Each node has a distinct value between 1 and 1000.
    • to_delete.length <= 1000
    • to_delete contains distinct values between 1 and 1000.

     

    【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. vector delNodes(TreeNode* root, vector<int>& to_delete) {
    15. vector forest;
    16. unordered_set<int> dict(to_delete.begin(), to_delete.end());
    17. root = helper(root, dict, forest);
    18. if (root) {
    19. forest.push_back(root);
    20. }
    21. return forest;
    22. }
    23. TreeNode* helper(TreeNode* root,
    24. const unordered_set<int>& dict,
    25. vector& forest) {
    26. if (!root) {return root;}
    27. root->left = helper(root->left, dict, forest);
    28. root->right = helper(root->right, dict, forest);
    29. if (dict.count(root->val)) {
    30. if (root->left) {forest.push_back(root->left);}
    31. if (root->right) {forest.push_back(root->right);}
    32. root = nullptr;
    33. }
    34. return root;
    35. }
    36. };

    【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 List delNodes(TreeNode root, int[] to_delete) {
    18. List forest = new ArrayList<>();
    19. Set dict = new HashSet() {{
    20. for (int num : to_delete) {add(num);}
    21. }};
    22. root = helper(root, dict, forest);
    23. if (root != null) {
    24. forest.add(root);
    25. }
    26. return forest;
    27. }
    28. TreeNode helper(TreeNode root,
    29. Set dict,
    30. List forest) {
    31. if (root == null) {return root;}
    32. root.left = helper(root.left, dict, forest);
    33. root.right = helper(root.right, dict, forest);
    34. if (dict.contains(root.val)) {
    35. if (root.left != null) {forest.add(root.left);}
    36. if (root.right != null) {forest.add(root.right);}
    37. root = null;
    38. }
    39. return root;
    40. }
    41. }

    参考文献

    【1】Java中集合与数组之间的转换方法_跨过七海之风的博客-CSDN博客_java集合转数组的方法

    【2】Java 几种方法初始化 Set_长安明月的博客-CSDN博客_java set初始化

  • 相关阅读:
    计算机毕业设计Java毕业生就业管理系统(源码+系统+mysql数据库+lw文档)
    含有稀土元素Tb荧光磁性高分子微球/可聚合端基双键铕多元配位复合物荧光微球的研究
    qmake 脚本 常用变量
    深度学习——深度学习计算二
    百度SEO优化(提升网站排名的秘诀)
    《Web安全基础》05. XSS · CSRF · SSRF · RCE
    ALlegro怎么恢复到初始操作界面?
    关于XCP标定CANape的界面安全会话DLL库
    MYSQL高级篇-----查询截取分析,锁机制,主从复制
    MFC Windows 程序设计[218]之网络打印机(附源码)
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126320648
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号