标签:Distance right val map int 863 Tree root left
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; } }
标签:Distance,right,val,map,int,863,Tree,root,left 来源: https://www.cnblogs.com/feiflytech/p/16142917.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。