Given a binary tree, a target node in the binary tree, and an integer value k, print all the nodes that are at distance k from the given target node. No parent pointers are available

Consider the tree shown in diagram

Input: target = pointer to node with data 8.
       root = pointer to node with data 20.
       k = 2.
Output : 10 14 22

If target is 14 and k is 3, then output should be "4 20"

from: http://www.geeksforgeeks.org/print-nodes-distance-k-given-node-binary-tree/

与该点相距k的点,分两种:

1. 在k的子树;

2. k回溯到前第d个点,然后从该点往左子树或者右子树求第k-d个点。

中序遍历,记录和target的距离。

每个点最多会被访问两次。所以还是O(n).

Print all nodes at distance k from a given node的更多相关文章

  1. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  2. [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...

  3. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  4. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  5. [Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon

    We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a ...

  6. leetcode 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  7. [LC] 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  8. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  9. cat hesA/Models/score_tgt.sc| awk '{ print $2,$19}' | sort -n -k 1

    cat hesA/Models/score_tgt.sc| awk '{ print $2,$19}' | sort -n -k 1 print the second collum in order

随机推荐

  1. ASCIL码和字符的转换

    1.在python中: 字符-->ASCIL 用ord函数 ASCIL-->字符 用chr函数 下面是一个输入小写字母转换为大写字母输出的例子: # -*- coding: utf-8 - ...

  2. AngularJS $http

    $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据.在服务器上读取数据: <div ng-app="myApp" ng-controller=&q ...

  3. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  4. 使用python实现栈和队列

    1.使用python实现栈: class stack(): def __init__(self): self.stack = [] def empty(self): return self.stack ...

  5. IDE-Sublime【1】-JsFormat插件格式化缩进问题

    JsFormat插件格式化Javascript代码的快捷键是ctrl+alt+f,发现默认缩进2个空格,但习惯上是4个空格,这里需要手动设置一下. 操作步骤: 1.打开Preferences-> ...

  6. [R语言]forecast.Arima中使用xreg报错

    问题: 使用forecast.Arima对带xreg的arima模型进行预测,报xreg Error pre.m4x <- forecast.Arima(m4x, h = 20, xreg = ...

  7. 【转】vim格式化C代码

    转自:http://blog.chinaunix.net/uid-24774106-id-3396220.html 在自己的目录下编辑自己的.vimrc, vim ~/.vimrc 添加下面的几行: ...

  8. BZOJ4521: [Cqoi2016]手机号码

    Description 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不 吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号 码单 ...

  9. fiddler 拦截小结

    一,拦截请求或响应常用命令 1.拦截命令 bpu 清除拦截请求 bpu http://www.baidu.com 拦截访问百度网站的请求.可以在 request 框的 WebForms 中改请求内容. ...

  10. GPIO裸机编程

    作者:李老师,华清远见嵌入式学院讲师. GPIO控制技术是接口技术中最简单的一种.本章通过介绍S5PV210芯片的GPIO控制方法,让读者初步掌握控制硬件接口的方法.本章的主要内容: GPIO功能介绍 ...