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. AngularJS学习之HTML DOM

    1.AngularJS为HTML DOM元素的属性提供了绑定应用数据的指令: 2.ng-disabled指令:直接绑定应用程序数据到HTML的disable属性: <div ng-app=&qu ...

  2. iOS10 UI教程视图和子视图的可见性

    iOS10 UI教程视图和子视图的可见性 iOS10 UI教程视图和子视图的可见性,一个父视图可以通过clipsToBounds属性,定义子视图在边界(边界就是父视图的框架也就是父视图可以显示的范围) ...

  3. 点击不同按钮生成不同窗体到某个panel上面,类似Frame用法--不错

    //点击不同按钮生成不同页面加载到某个panel上面.procedure TMainForm.DemoButtonClick(Sender: TObject); var NewDemoClass: T ...

  4. 遍历table指定name的td

    $("td[name='rates']").each(function () { var temp = $(this).text().substr(0,$(this).text() ...

  5. 关于__IPHONE_OS_VERSION_MAX_ALLOWED和__IPHONE_OS_VERSION_MIN_ALLOWED的用法

    简单来说, 这些编译期常量, 对运行时的环境判断完全无效, 它告诉编译器用哪一段代码来进行编译, 那那段代码里, 你仍然可以写运行到目标机器里会崩溃的代码, 从stackoverflow里拷两段示例代 ...

  6. MFC 单文档 根据数据 绘图

    以VS2015中创建SDI为例 选择生成的类为:C***View,基类为CView,***为项目名 在***Doc.h和***Doc.cpp中创建数据模型,在***View.cpp的OnDraw()中 ...

  7. BZOJ4380 : [POI2015]Myjnie

    将$c$离散化,设: $f[i][j][k]$为区间$[i,j]$最小值为$k$的最大收益. $g[i][j][k]$为$\max(g[i][j][k..m])$. $h[i][j]$为对于当前DP区 ...

  8. Storm系统高可用性HA表现

    关于Storm的高可用,有以下几个方面: (1)数据利用阶段可以通过ACK机制保证数据被处理: (2)在进程级别,worker失效,supervisor会自动重启worker线程; (3)在组件级别, ...

  9. UVa 10806 & 费用流+意识流...

    题意: 一张无向图,求两条没有重复的从S到T的路径. SOL: 网络流为什么屌呢..因为网络流的容量,流量,费用能对许许多多的问题进行相应的转化,然后它就非常的屌. 对于这道题呢,不是要没有重复吗?不 ...

  10. Android 开源项目

    StickerCamera 一个完整的开源项目.贴纸标签相机(类似nice,in),拍照,裁剪,贴贴纸打标签功能. MD-BiliBili 基于 Material Design 的 BiliBili ...