Print all nodes at distance k from a given node
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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 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
随机推荐
- node.js整理 07例子
需求 一个简单的静态文件合并服务器,该服务器需要支持类似以下格式的JS或CSS文件合并请求. http://assets.example.com/foo/??bar.js,baz.js 在以上URL中 ...
- DOM--6 向应用程序中加入ajax
组合技术 适当的ajax时对已有技术和下列思想的组合 语义化(X)HTML标记 文档对象模型(DOM) JavaScript XML 不同浏览器中,公共XMLHttpRequest方法 open(me ...
- js从字符串中提取身份证号,连续18位数字
<!DOCTYPE html> <html> <head> <title>提取身份证号</title> <meta charset=& ...
- Android自动化测试 - MonkeyRunner(三) 随手练习测试脚本
#coding=utf-8 import os import time #import MonkeyRunner three module from com.android.monkeyrunner ...
- LightOJ1230 Placing Lampposts(DP)
题目大概说给一个森林求其最小点覆盖数,同时在最小点覆盖条件下输出最多有多少条边被覆盖两次. dp[0/1][u]表示以u为根的子树内的边都被覆盖且u不属于/属于覆盖集所需的最少点数 另外,用cnt[0 ...
- java基础-表达式,语句和代码块
浏览以下内容前,请点击并阅读 声明 表达式 表达式由变量,操作符和方法调用组成,表达式的返回值类型由表达式中的元素(如操作符,变量等)决定如: cadence = 0 上述代码将返回一个int类型的值 ...
- Shell 脚本
近期在别人的工作基础上完善了几个shell自动安装脚本. 1. 循环远程访问机器并安装 #!/bin/bash IpPrefix=. User=root Pwd= SMNIP=52.1.123.79 ...
- ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报
Mr. Kitayuta's Colorful GraphTime Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 【BZOJ】2675: Bomb
题意: 给n个点,任选其中3个点(一个点只能取一次),求选出三个点的最大曼哈顿距离之和与最小曼哈顿距离之和(n<=10^5). 题解: 最大曼哈顿距离之和很好求,就是能包围所有点的经过三个点的矩 ...
- 【BZOJ】3712: [PA2014]Fiolki
http://www.lydsy.com/JudgeOnline/problem.php?id=3712 题意:n个瓶子,第i个瓶子里又g[i]克物质.m次操作,第i次操作把第a[i]个瓶子的东西全部 ...