Print all nodes that are at distance k from a leaf node
来源:互联网 发布:阿里云邮件推送 编辑:程序博客网 时间:2024/05/21 17:15
转自:http://www.geeksforgeeks.org/print-nodes-distance-k-leaf-node/
Given a Binary Tree and a positive integer k, print all nodes that are distance k from a leaf node.
Here the meaning of distance is different from previous post. Here k distance from a leaf means k levels higher than a leaf node. For example if k is more than height of Binary Tree, then nothing should be printed. Expected time complexity is O(n) where n is the number nodes in the given Binary Tree.
We strongly recommend to minimize the browser and try this yourself first.
The idea is to traverse the tree. Keep storing all ancestors till we hit a leaf node. When we reach a leaf node, we print the ancestor at distance k. We also need to keep track of nodes that are already printed as output. For that we use a boolean array visited[].
/* Program to print all nodes which are at distance k from a leaf */
#include <iostream>
using
namespace
std;
#define MAX_HEIGHT 10000
struct
Node
{
int
key;
Node *left, *right;
};
/* utility that allocates a new Node with the given key */
Node* newNode(
int
key)
{
Node* node =
new
Node;
node->key = key;
node->left = node->right = NULL;
return
(node);
}
/* This function prints all nodes that are distance k from a leaf node
path[] --> Store ancestors of a node
visited[] --> Stores true if a node is printed as output. A node may be k
distance away from many leaves, we want to print it once */
void
kDistantFromLeafUtil(Node* node,
int
path[],
bool
visited[],
int
pathLen,
int
k)
{
// Base case
if
(node==NULL)
return
;
/* append this Node to the path array */
path[pathLen] = node->key;
visited[pathLen] =
false
;
pathLen++;
/* it's a leaf, so print the ancestor at distance k only
if the ancestor is not already printed */
if
(node->left == NULL && node->right == NULL &&
pathLen-k-1 >= 0 && visited[pathLen-k-1] ==
false
)
{
cout << path[pathLen-k-1] <<
" "
;
visited[pathLen-k-1] =
true
;
return
;
}
/* If not leaf node, recur for left and right subtrees */
kDistantFromLeafUtil(node->left, path, visited, pathLen, k);
kDistantFromLeafUtil(node->right, path, visited, pathLen, k);
}
/* Given a binary tree and a nuber k, print all nodes that are k
distant from a leaf*/
void
printKDistantfromLeaf(Node* node,
int
k)
{
int
path[MAX_HEIGHT];
bool
visited[MAX_HEIGHT] = {
false
};
kDistantFromLeafUtil(node, path, visited, 0, k);
}
/* Driver program to test above functions*/
int
main()
{
// Let us create binary tree given in the above example
Node * root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);
cout <<
"Nodes at distance 2 are: "
;
printKDistantfromLeaf(root, 2);
return
0;
}
Output:
Nodes at distance 2 are: 3 1
Time Complexity: Time Complexity of above code is O(n) as the code does a simple tree traversal.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Print all nodes that are at distance k from a leaf node
- 打印距离叶子节点为k的节点 Print nodes that are at distance k from a leaf node
- Print all possible strings of length k that can be formed from a set of n characters
- BFS print BT all path from root to leaf
- Shortest Distance from All Buildings
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- FWNX-creates a linked list of all the nodes at each depth-JAVA VERSION
- Q 4.4 creates a linked list of all the nodes at each depth
- What are all the common undefined behaviour that a C++ programmer should know about?
- All the TCP/IP ports that are in a TIME_WAIT status
- 41 Which statements are true regarding table compression? (Choose all that apply.) A. It saves disk
- uva548 -treeDescription Download as PDF You are to determine the value of the leaf node in a given
- you are calling a UIKit method that can only be invoked from the UI thread.
- Print Boundry Nodes of a binary tree.
- 4 4 Given a binary search tree, design an algorithm which creates a linked list of all the nodes at
- [CrackCode] 4.4 Creates a linked list of all the nodes at each depth of a given tree
- [LeetCode317]Shortest Distance from All Buildings
- Leetcode: Shortest Distance from All Buildings
- 互联网之子–Aaron Swartz
- CentOS编译OpenJDK
- 数据结构实验2——链表
- javascript var
- 基于ARM的nucleus plus的执行
- Print all nodes that are at distance k from a leaf node
- 用python写pintool
- 网络基本功(三):细说VLAN与Trunk
- 《设计模式之禅》笔记16 -- 组合模式
- myeclipse2014关闭html,jsp等页面的可视化编辑器
- Spring Framework源码(八):SpringMVC概览
- 设计模式几点原则
- 遍历可变数组的同时删除元素
- Spring(三) spring对jdbc的封装