Hi,
Construct a BST with 2,1,3. If my understanding is right, now distance
between 2,3 will be 2. Will this algo return the correct answer?.
On Thu, Feb 2, 2012 at 1:43 PM, Manni mbd mbd2...@gmail.com wrote:
Forget my previous post. it useless i think
Firstly there will be only 1 node
Forget my previous post. it useless i think
Firstly there will be only 1 node upwards which will be at a distance
K units from the specified node.
so we can take help of recursion
int kthDistance(node *root, int k ,node *start){
static node * kthUp = NULL;
int distance = -1;
if(node ==NULL)
^same as above..
for upward.. start again from the nodes now distance is distance is
(distance of start node -k) .. if you reach this from the root.. print
it..
also better is we use array rather than using linked list .. as
sorting can be a tedious task in case of link lists !
On 2/1/12, atul
@Manni : nodes should be added to the linklist ..such that linklist remain
in sorted orderno need to sort the linklist.
On Wed, Feb 1, 2012 at 2:30 PM, Manni mbd mbd2...@gmail.com wrote:
^same as above..
for upward.. start again from the nodes now distance is distance is
(distance of
@Manni : didnt get your algo for upward nodes.
On Wed, Feb 1, 2012 at 2:30 PM, Manni mbd mbd2...@gmail.com wrote:
^same as above..
for upward.. start again from the nodes now distance is distance is
(distance of start node -k) .. if you reach this from the root.. print
it..
also better is
You are given a function printKDistanceNodes which takes in a root node of
a binary tree, a start node and an integer K. Complete the function to
print the value of all the nodes (one-per-line) which are a K distance from
the given start node in sorted order. Distance can be upwards or downwards.
are you sure given tree is binary tree and not BST.
if it is BST then we can search start node and then do inorder traversal
from there.
before thinking printing abt upward node...please confirm if it a binary
tree or BST.
On Tue, Jan 31, 2012 at 9:27 PM, Dhirendra Singh dps...@gmail.com wrote:
if it is binary tree then to print the downward node...
we can search for start node and then do level-order traversal or BFS from
start node till distance K recursively.
no as we want nodes to be printed in sorted order..what we do is crated a
linked-list and insert nodes (found in above method)