863. All Nodes Distance K in Binary Tree
2022/4/14 9:13:04
本文主要是介绍863. All Nodes Distance K in Binary Tree,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
The key to solve this problem is to find the path from root to target, and put the length to target of every node from root to target to a map.
Then either using BFS or using DFS depends on you.
BFS:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { List<Integer> res = new ArrayList<>(); public List<Integer> distanceK(TreeNode root, TreeNode target, int k) { Map<Integer, Integer> map = new HashMap<>(); preOrder(root, target.val, map); bfs(root, k, map); return res; } private void bfs(TreeNode root, int k, Map<Integer, Integer> map){ Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()){ int size = queue.size(); for(int i=0;i<size;i++){ TreeNode node = queue.poll(); int len=0; if(map.containsKey(node.val)){ len = map.get(node.val); if(len==k){ res.add(node.val); } } if(node.left!=null){ if(!map.containsKey(node.left.val)) map.put(node.left.val, len+1); queue.offer(node.left); } if(node.right!=null){ if(!map.containsKey(node.right.val)) map.put(node.right.val, len+1); queue.offer(node.right); } } } } private int preOrder(TreeNode root, int target, Map<Integer, Integer> map){ if(root==null) return -1; if(root.val==target){ map.put(root.val, 0); return 0; } int left = preOrder(root.left, target, map); int right = preOrder(root.right, target, map); if(left>=0){ map.put(root.val, left+1); return left+1; } if(right>=0){ map.put(root.val, right+1); return right+1; } return -1; } }
DFS:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { List<Integer> res = new ArrayList<>(); public List<Integer> distanceK(TreeNode root, TreeNode target, int k) { Map<Integer, Integer> map = new HashMap<>(); preOrder(root, target.val, map); dfs(root, k, map); return res; } private void dfs(TreeNode root, int k, Map<Integer, Integer> map){ if(map.get(root.val)==k){ res.add(root.val); } if(root.left!=null){ if(!map.containsKey(root.left.val)){ map.put(root.left.val, map.get(root.val)+1); } dfs(root.left, k, map); } if(root.right!=null){ if(!map.containsKey(root.right.val)){ map.put(root.right.val, map.get(root.val)+1); } dfs(root.right, k, map); } } private int preOrder(TreeNode root, int target, Map<Integer, Integer> map){ if(root==null) return -1; if(root.val==target){ map.put(root.val, 0); return 0; } int left = preOrder(root.left, target, map); int right = preOrder(root.right, target, map); if(left>=0){ map.put(root.val, left+1); return left+1; } if(right>=0){ map.put(root.val, right+1); return right+1; } return -1; } }
这篇关于863. All Nodes Distance K in Binary Tree的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc
- 2024-06-17zero-shot-learning-definition-examples-comparison
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享