首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
距离是K的二叉树节点
[编程题]距离是K的二叉树节点
热度指数:609
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个二叉树的根节点 root ,和一个目标节点的值 target ,和一个目标距离 k ,请你找出二叉树上所有与 target 距离是 k 的节点的值。
数据范围:二叉树的节点数满足
,节点上的值在范围 [0,n) 内,每个节点的值各不相同。
示例1
输入
{3,5,2,4,6,0,7,1,8},5,2
输出
[1,8,2]
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(23)
分享
纠错
提交结果有问题?
6个回答
5篇题解
开通博客
王颙卬
发表于 2022-05-29 15:25:10
class Solution { public: unordered_map<int,TreeNode*>parent; vector<int>ans; &nb
展开全文
17c89
发表于 2024-04-20 15:31:43
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int v
展开全文
奶ve
发表于 2024-05-12 06:47:40
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullpt
展开全文
NekoNyaNya
发表于 2024-04-30 17:06:57
#include <unordered_map> #include <vector> class Solution { private: int k_{}; int target_{}; vector<int> res_; unor
展开全文
姐姐的遮阳伞
发表于 2022-04-06 22:55:50
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int v
展开全文
问题信息
树
dfs
广度优先搜索(BFS)
难度:
6条回答
23收藏
1761浏览
热门推荐
通过挑战的用户
查看代码
Secret1...
2022-09-15 21:27:50
半个西瓜半个夏
2022-09-15 10:43:10
FM_NowC...
2022-09-08 09:00:10
外骨骼
2022-09-07 00:42:26
fred-coder
2022-09-04 17:15:13
相关试题
回路
dfs
评论
(51)
分支限界法与回溯法的相同点是()
dfs
评论
(4)
来自
360公司2016研发工...
利用回溯法求下列不等式的所有整数解...
dfs
评论
(10)
来自
乐视2017秋招开发工程...
之前的经历中单品数据分析的经验丰富...
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型ArrayList */ public ArrayList
distanceKnodes (TreeNode root, int target, int k) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型vector */ vector
distanceKnodes(TreeNode* root, int target, int k) { // write code here } };
#coding:utf-8 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @param target int整型 # @param k int整型 # @return int整型一维数组 # class Solution: def distanceKnodes(self , root , target , k ): # write code here
using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ public List
distanceKnodes (TreeNode root, int target, int k) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ function distanceKnodes( root , target , k ) { // write code here } module.exports = { distanceKnodes : distanceKnodes };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ function distanceKnodes( $root , $target , $k ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @param target int整型 # @param k int整型 # @return int整型一维数组 # class Solution: def distanceKnodes(self , root: TreeNode, target: int, k: int) -> List[int]: # write code here
package main //import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ func distanceKnodes( root *TreeNode , target int , k int ) []int { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* distanceKnodes(struct TreeNode* root, int target, int k, int* returnSize ) { // write code here }
# class TreeNode # attr_accessor :val, :left, :right # def initialize(val, left = nil, right = nil) # @val, @left, @right = val, left, right # end # end # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @param target int整型 # @param k int整型 # @return int整型一维数组 # class Solution def distanceKnodes(root, target, k) # write code here end end
/** * class TreeNode(var `val`: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ def distanceKnodes(root: TreeNode,target: Int,k: Int): Array[Int] = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ fun distanceKnodes(root: TreeNode?,target: Int,k: Int): IntArray { // write code here } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ public int[] distanceKnodes (TreeNode root, int target, int k) { // write code here } }
/*class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ export function distanceKnodes(root: TreeNode, target: number, k: number): number[] { // write code here }
/** * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int=0, _ left: TreeNode?=nil, _ right: TreeNode?=nil) { * self.val = val * self.left = left * self.right = right * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ func distanceKnodes ( _ root: TreeNode?, _ target: Int, _ k: Int) -> [Int] { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option
>, * pub right: Option
>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组 */ pub fn distanceKnodes(&self, root: Option
>, target: i32, k: i32) -> Vec
{ // write code here } }
{3,5,2,4,6,0,7,1,8},5,2
[1,8,2]