863:二叉树中所有距离为 K 的结点(未完成)

This commit is contained in:
huangge1199 2021-07-28 17:02:38 +08:00
parent 6e631a0604
commit 16080bac5b
4 changed files with 192 additions and 1 deletions

View File

@ -2,7 +2,9 @@ package com.code.leet.entiy;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @Author: hyy
@ -70,4 +72,43 @@ public class TreeNode {
}
}
}
public TreeNode preTreeNode(List<Integer> list) {
if (list == null || list.size() == 0) {
return null;
}
TreeNode root = new TreeNode(list.get(0));
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
for (int i = 1; i < list.size(); i = i + 2) {
TreeNode node = queue.poll();
Integer left = list.get(i);
if (node == null) {
queue.add(null);
queue.add(null);
} else {
if (left != null) {
node.left = new TreeNode(left);
}
queue.add(node.left);
Integer right = i + 1 < list.size() ? list.get(i + 1) : null;
if (right != null) {
node.right = new TreeNode(right);
}
queue.add(node.right);
}
}
return root;
}
public TreeNode search(TreeNode root, int target) {
if (root == null || root.val == target) {
return root;
}
TreeNode temp = search(root.left, target);
if (temp != null) {
return temp;
}
return search(root.right, target);
}
}

View File

@ -0,0 +1,116 @@
//给定一个二叉树具有根结点 root 一个目标结点 target 和一个整数值 K
//
// 返回到目标结点 target 距离为 K 的所有结点的值的列表 答案可以以任何顺序返回
//
//
//
//
//
//
// 示例 1
//
// 输入root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
//输出[7,4,1]
//解释
//所求结点为与目标结点值为 5距离为 2 的结点
//值分别为 74以及 1
//
//
//
//注意输入的 "root" "target" 实际上是树上的结点
//上面的输入仅仅是对这些对象进行了序列化描述
//
//
//
//
// 提示
//
//
// 给定的树是非空的
// 树上的每个结点都具有唯一的值 0 <= node.val <= 500
// 目标结点 target 是树上的结点
// 0 <= K <= 1000.
//
// Related Topics 深度优先搜索 广度优先搜索 二叉树
// 👍 354 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.*;
//863:二叉树中所有距离为 K 的结点
public class AllNodesDistanceKInBinaryTree {
public static void main(String[] args) {
//测试代码
Solution solution = new AllNodesDistanceKInBinaryTree().new Solution();
TreeNode root = new TreeNode();
root = root.preTreeNode(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4));
solution.distanceK(root, root.search(root, 5), 2);
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private Stack<TreeNode> stack = new Stack<>();
List<Integer> list = new ArrayList<>();
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
if (k == 0) {
return Arrays.asList(target.val);
}
if (!root.val.equals(target.val)) {
serchTarget(root, target.val, true);
}
int index = 1;
while (!stack.isEmpty() && k >= index) {
TreeNode node = stack.pop();
dfs(node, 0, k - index);
index++;
}
return list;
}
private boolean serchTarget(TreeNode root, int val, boolean bl) {
if (root == null) {
return true;
}
if (root.val == val) {
return false;
}
if (bl) {
stack.push(root);
}
bl = serchTarget(root.left, val, bl) || serchTarget(root.right, val, bl);
if (bl) {
stack.pop();
}
return bl;
}
private void dfs(TreeNode root, int index, int k) {
if (root == null) {
return;
}
if (index == k) {
list.add(root.val);
return;
}
dfs(root.left, index + 1, k);
dfs(root.right, index + 1, k);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,34 @@
<p>给定一个二叉树(具有根结点&nbsp;<code>root</code>&nbsp;一个目标结点&nbsp;<code>target</code>&nbsp;,和一个整数值 <code>K</code></p>
<p>返回到目标结点 <code>target</code> 距离为 <code>K</code> 的所有结点的值的列表。 答案可以以任何顺序返回。</p>
<p>&nbsp;</p>
<ol>
</ol>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
<strong>输出:</strong>[7,4,1]
<strong>解释:</strong>
所求结点为与目标结点(值为 5距离为 2 的结点,
值分别为 74以及 1
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png" style="height: 240px; width: 280px;">
注意,输入的 &quot;root&quot;&quot;target&quot; 实际上是树上的结点。
上面的输入仅仅是对这些对象进行了序列化描述。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li>给定的树是非空的。</li>
<li>树上的每个结点都具有唯一的值&nbsp;<code>0 &lt;= node.val &lt;= 500</code>&nbsp;</li>
<li>目标结点&nbsp;<code>target</code>&nbsp;是树上的结点。</li>
<li><code>0 &lt;= K &lt;= 1000</code>.</li>
</ol>
<div><div>Related Topics</div><div><li></li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div>\n<div><li>👍 354</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long