863. All Nodes Distance K in Binary Tree Medium
1/**
2 * [863] All Nodes Distance K in Binary Tree
3 *
4 * Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node.
5 * You can return the answer in any order.
6 *
7 * Example 1:
8 * <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png" style="width: 500px; height: 429px;" />
9 * Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
10 * Output: [7,4,1]
11 * Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
12 *
13 * Example 2:
14 *
15 * Input: root = [1], target = 1, k = 3
16 * Output: []
17 *
18 *
19 * Constraints:
20 *
21 * The number of nodes in the tree is in the range [1, 500].
22 * 0 <= Node.val <= 500
23 * All the values Node.val are unique.
24 * target is the value of one of the nodes in the tree.
25 * 0 <= k <= 1000
26 *
27 */
28pub struct Solution {}
29use crate::util::tree::{TreeNode, to_tree};
30
31// problem: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
32// discuss: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36// Definition for a binary tree node.
37// #[derive(Debug, PartialEq, Eq)]
38// pub struct TreeNode {
39// pub val: i32,
40// pub left: Option<Rc<RefCell<TreeNode>>>,
41// pub right: Option<Rc<RefCell<TreeNode>>>,
42// }
43//
44// impl TreeNode {
45// #[inline]
46// pub fn new(val: i32) -> Self {
47// TreeNode {
48// val,
49// left: None,
50// right: None
51// }
52// }
53// }
54use std::rc::Rc;
55use std::cell::RefCell;
56impl Solution {
57 pub fn distance_k(root: Option<Rc<RefCell<TreeNode>>>, target: Option<Rc<RefCell<TreeNode>>>, k: i32) -> Vec<i32> {
58 vec![]
59 }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_863() {
70 }
71}
72
Back
© 2025 bowen.ge All Rights Reserved.