}
if(++count == remainder) {
queue.stream().forEach(a->result.add(a.val));
break;
}
}
}else if(remainder == 0) {
result.add(currentNode.val);
}
}
}
public void findDown(int k, TreeNode startNode, List result) {
if(k == 0) {//findUp不用,只一个即可
result.add(startNode.val);
return;
}
LinkedList queue = new LinkedList<>();
queue.offer(startNode);
int count = 0;
while(!queue.isEmpty()) {
for(int size = queue.size(); size > 0; size–) {
TreeNode node = queue.poll();
if(node.left != null)
queue.offer(node.left);
if(node.right != null)
queue.offer(node.right);
}
if(++count == k) {
queue.stream().forEach(a->result.add(a.val));
break;
}
}
}
//目标节点到跟节点的路径
@SuppressWarnings(“unchecked”)
public LinkedList findTarget(TreeNode root, TreeNode target){
LinkedList<Object[]> stack = new LinkedList<>();
stack.push(new Object[] {root, new LinkedList()});
while(!stack.isEmpty()) {
Object[] arr = stack.pop();
TreeNode node = (TreeNode)arr[0];
LinkedList path = (LinkedList)arr[1];
path.add(node);
if(node.val == target.val)
return path;
if(node.left != null && node.right == null) {
stack.push(new Object[] {node.left, path});
}
if(node.left == null && node.right != null) {
stack.push(new Object[] {node.right, path});
}
if(node.left != null && node.right != null) {
stack.push(new Object[] {node.right, new LinkedList(path)});
stack.push(new Object[] {node.left, path});
}
}
return new LinkedList();
}
//方法二:别人写的
public static class Solution1{
Map<TreeNode, Integer> map = new HashMap<>();
public List distanceK(TreeNode root, TreeNode target, int K) {
List res = new LinkedList<>();
find(root, target);
dfs(root, K, map.get(root), res);
return res;
}
// find target node first and store the distance in that path that we could use it later directly
private int find(TreeNode root, TreeNode target) {
if (root == null) return -1;
if (root.val == target.val) {
map.put(root, 0);
return 0;
}
int left = find(root.left, target);
if (left >= 0) {
map.put(root, left + 1);
return left + 1;
}
int right = find(root.right, target);
if (right >= 0) {
map.put(root, right + 1);
return right + 1;
}
return -1;
}
private void dfs(TreeNode root, int K, int length, List res) {
if (root == null) return;
if (map.containsKey(root)) length = map.get(root);
if (length == K) res.add(root.val);
dfs(root.left, K, length + 1, res);
dfs(root.right, K, length + 1, res);
}
}
//方法三:别人写的2,方法二缝合版
public static class Solution2{
public List distanceK(TreeNode root, TreeNode target, int K) {
List res = new LinkedList<>();
if (K == 0) {
res.add(target.val);
} else {
dfs(res, root, target.val, K ,0);
}
return res;
}
private int dfs(List res, TreeNode node, int target, int K, int depth) {
if (node == null) return 0;
if (depth == K) {
res.add(node.val);
return 0;
}
int left, right;
if (node.val == target || depth > 0) {
left = dfs(res, node.left, target, K, depth + 1);
right = dfs(res, node.right, target, K, depth + 1);
} else {
left = dfs(res, node.left, target, K, depth);
right = dfs(res, node.right, target, K, depth);
}
if (node.val == target) return 1;
if (left == K || right == K) {
res.add(node.val);
return 0;
}
if (left > 0) {
dfs(res, node.right, target, K, left + 1);
return left + 1;
}
if (right > 0) {
dfs(res, node.left, target, K, right + 1);
return right + 1;
}
return 0;
}
}
}
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.hamcrest.collection.IsEmptyCollection;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Test;
import com.lun.medium.AllNodesDistanceKInBinaryTree.Solution1;
import com.lun.medium.AllNodesDistanceKInBinaryTree.Solution2;
import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;
public class AllNodesDistanceKInBinaryTreeTest {
@Test
public void test() {
AllNodesDistanceKInBinaryTree obj = new AllNodesDistanceKInBinaryTree();
TreeNode root = BinaryTree.integers2BinaryTree(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
assertThat(obj.distanceK(root, new TreeNode(5), 2), IsIterableContainingInAnyOrder.containsInAnyOrder(7,4,1));
}
@Test
public void test2() {
Solution1 obj = new Solution1();
TreeNode root = BinaryTree.integers2BinaryTree(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
assertThat(obj.distanceK(root, new TreeNode(5), 2), //
IsIterableContainingInAnyOrder.containsInAnyOrder(7,4,1));
}
@Test
public void test3() {
Solution2 obj = new Solution2();
TreeNode root = BinaryTree.integers2BinaryTree(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
assertThat(obj.distanceK(root, new TreeNode(5), 2), //
IsIterableContainingInAnyOrder.containsInAnyOrder(7,4,1));
}
@Test
public void testFind() {
AllNodesDistanceKInBinaryTree obj = new AllNodesDistanceKInBinaryTree();
TreeNode root = BinaryTree.integers2BinaryTree(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
StringBuilder sb = new StringBuilder();
obj.findTarget(root, new TreeNode(4)).forEach(a->sb.append(a.val+“,”));
assertEquals(“3,5,2,4,”, sb.toString());
StringBuilder sb2 = new StringBuilder();
obj.findTarget(root, new TreeNode(8)).forEach(a->sb2.append(a.val+“,”));
assertEquals(“3,1,8,”, sb2.toString());
StringBuilder sb3 = new StringBuilder();
obj.findTarget(root, new TreeNode(5)).forEach(a->sb3.append(a.val+“,”));
assertEquals(“3,5,”, sb3.toString());
assertThat(obj.findTarget(root, new TreeNode(9)), IsEmptyCollection.empty());
StringBuilder sb4 = new StringBuilder();
obj.findTarget(root, new TreeNode(3)).forEach(a->sb4.append(a.val+“,”));
assertEquals(“3,”, sb4.toString());
}
@Test
public void testFindUp() {
AllNodesDistanceKInBinaryTree obj = new AllNodesDistanceKInBinaryTree();
TreeNode root = BinaryTree.integers2BinaryTree(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
LinkedList path = obj.findTarget(root, new TreeNode(5));
List result = new ArrayList<>();
obj.findUp(3, path, result);
assertThat(result, IsIterableContainingInAnyOrder.containsInAnyOrder(0, 8));
List result2 = new ArrayList<>();
obj.findUp(2, path, result2);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-1lG16yu8-1715551164881)]
[外链图片转存中…(img-7u1cLzu7-1715551164882)]
[外链图片转存中…(img-9CIj2Eeb-1715551164882)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!