查看: 1219| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Java/C#] 863题的DFS 解法,附上代码

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
这题大家觉得好做吗?  (仅讨论DFS的做法)

我想得头都快炸了。

  1. class Solution {

  2.         Map<TreeNode, Integer> map = new HashMap<>();
  3.    
  4.         public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
  5.             List<Integer> res = new ArrayList<>();
  6.             find(root, target);
  7.             search(root, 0, K, res);
  8.             return res;
  9.         }

  10.         private void find(TreeNode root, TreeNode target) {
  11.             
  12.             if (root == null) {
  13.                 return;
  14.             }

  15.             if (root == target) {
  16.                 map.put(root, 0);
  17.                 return;
  18.             }

  19.             find(root.left, target);
  20.             if (map.containsKey(root.left)) {
  21.                 map.put(root, map.get(root.left) + 1);
  22.                 return;
  23.             }

  24.             find(root.right, target);
  25.             if (map.containsKey(root.right)) {
  26.                 map.put(root, map.get(root.right) + 1);
  27.                 return;
  28.             }
  29.             return;
  30.             
  31.         }

  32.         public void search(TreeNode root, int dis, int K, List<Integer> res) {
  33.             
  34.             if (root == null) {
  35.                 return;
  36.             }

  37.             if (map.containsKey(root)) {
  38.                 dis = map.get(root);
  39.             }

  40.             if (dis == K) {
  41.                 res.add(root.val);
  42.             }

  43.             search(root.left, dis + 1, K, res);
  44.             search(root.right, dis + 1, K, res);
  45.             
  46.         }
  47. }
复制代码



上一篇:聊一些Python的坑,刷题的时候值得注意
下一篇:210题的 DFS 解法 好难理解
推荐
ashang83 2020-11-1 08:33:17 | 只看该作者
全局:
我猜你是卡在search的部分吧?
假設target 是在左子樹, 那每次往右子樹往下的時候distance 都是root distance +1, 這部分沒問題, 因為是離target愈來愈遠, 且find 的過程中所有的右子樹都不會被記錄在map, 這時候dis +1 就會被用到
但在左子樹就不是了
當你往下一層的時候有幾種情況
EX
          1
        2   3
     4  5    6
    7
target = 4
1. 靠近target, node = 2 時, distance = root distance - 1
2. 先靠近target, 再遠離target, 例如node 5 及其子樹
3. 越過target, 距離增加, 例如node 7 及其子樹
因此在左子樹的traverse中, update distance是必要的, 不能單純用dis + 1 來當作距離計算

评分

参与人数 2大米 +5 收起 理由
14417335 + 4
techDiscussion + 1 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

🔗
 楼主| techDiscussion 2020-11-1 08:46:45 | 只看该作者
全局:
ashang83 发表于 2020-11-1 08:33
我猜你是卡在search的部分吧?
假設target 是在左子樹, 那每次往右子樹往下的時候distance 都是root distan ...

谢谢同学,我卡的是dfs的 find 部分~~~~ 我觉得这个find太神奇了
回复

使用道具 举报

🔗
ashang83 2020-11-1 22:37:09 | 只看该作者
全局:
techDiscussion 发表于 2020-11-1 08:46
谢谢同学,我卡的是dfs的 find 部分~~~~ 我觉得这个find太神奇了

的確是
利用dfs 先找到target再往回update路徑上所有node的distance, 但又不觸及不需要的node
滿不錯的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表