码农知识堂 - 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初始化

  • 相关阅读:
    Leetcode162. 寻找峰值
    协同过滤算法
    R语言地理加权回归、主成份分析、判别分析等空间异质性数据分析
    贪心算法练习:数列极差问题
    docker-compose一键启动neo4j
    从实战出发,深入解读Netty底层核心源码,不得不说大佬就是大佬
    电子标准院工程师鲍薇:人工智能标准化引领产业发展
    神了,用 Python 预测世界杯决赛,发现准确率还挺高
    Java-数据类型与变量
    【juc学习之路第8天】Condition
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126320648
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号