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. 解决js(ajax)提交后端的“ _xsrf' argument missing from POST” 的错误

    首先先简述一下CSRF: CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意思,也就是在用户会话下对某个CGI做一些GET/POST ...

  2. iOS 获取当前用户的用户路径并写入文件

    NSString *path = [[@"~" stringByExpandingTildeInPath] stringByAppendingString: @"/tmp ...

  3. Git pull 强制覆盖本地文件

    git fetch --all git reset --hard origin/master git pull

  4. js 数组(Array)

    一.数组 稠密数组(非稀疏数组) 稀疏数组 二.数组的添加和删除 我们已经常见过添加数组元素最简单的方法:为新索引赋值;也可以使用push()方法在数组末尾增加一个或者多个元素: a = []; a. ...

  5. node.js链接mysql

    node.js连接数据库有很多种,比如:mongoose,oracle,mysql...,我自己玩就选了一个我很熟悉的轻量级的mysql数据库尝试了一把,感觉不错. 首先要把mysql客户端安装好,官 ...

  6. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout

    K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...

  7. Ignatius's puzzle

    Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. [R语言]foreach和doParallel包实现多个数据库同时查询

    R语言在进行数据库查询时,每执行一条语句,都会阻塞.直到查询语句返回结果之后,才会进行下一条语句. 为了能够实现同时对多个数据库进行查询,以节省顺序执行下来的时间,首先考虑通过多线程来进行数据库查询. ...

  9. [知识点]KMP算法

    // 此博文为迁移而来,写于2015年5月24日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w1iw.html 1.前 ...

  10. 【BZOJ】2434: [Noi2011]阿狸的打字机

    题意 给你一些字符串.\(m\)次询问,每一次询问第\(x\)个字符串在\(y\)字符串中出现了多少次.(输入总长$ \le 10^5$, \(M \le 10^5\)) 分析 在ac自动机上,\(x ...