If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1?
According to the Wikipedia definition of the Lowest common ancestor I correct myself:
The lowest common ancestor (LCA) is a concept in graph theory and
computer science. Let T be a rooted tree with n nodes. The lowest
common ancestor is defined between two nodes v and w as the lowest
node in T that has both v and w as descendants (where we allow a node
to be a descendant of itself).
So yes going by this definition the correct answer would be 6. If this is an interview question would be good to clarify in advance with the interviewer.
Related
I want to find maximum sum of K connected nodes in a binary tree. I thought of doing this through memorizing, but I'm stuck.
Think about how the problem breaks down. A connected subgraph of a binary tree is itself a tree. In particular, there is a root of the the subgraph, and the children of the root are also trees.
So, consider the following related problem. Given the binary tree and a particular node X, what is the maximum sum of a subtree rooted at X, where the subtree has N nodes?
If you could answer that question for N=1 through N=i, then you can answer the same question for N=i+1 by fixing the node X, choosing j nodes from X's left child, and i-j nodes from X's right child, for some choice of 0<=j<=i. This illustrates the "optimal subproblem" property required for dynamic programming.
I am asked to write an algorithm that finds the Minimum Spanning Tree in a graph G, but with the condition that each vertex of G be a leave in the spanning Tree T.
How can this be possible if the graph has more than 2 elements? Suppose G contains the vertices a,b and c, the Spanning tree will might something like a--b--c, so in this case b is not a leaf.
I am not looking for a solution to the algorithm, I only want to understand how a Spanning Tree can be composed exclusively of leaves.
Here is the exact wording of the question
Thanks for the help
The question states that S is a subset of the vertices V in the graph. There may be non-leaf nodes. However, you have to make sure that these internal nodes are not in S. If S would be equal to V you'd be right.
I came across THIS geeksforgeeks post to find nodes at distance k from the given node in a binary tree.
I am not able to understand it even after spending multiple hours. Specially the part to find the nodes at distance k in ancestors.
Can someone please please help me with a small dry run on the code/algorithm in the geeksforgeeks post? Or any other easy to understand solution without using parent pointer?
Let's say the depth of target node is D.
If the nodes you want is in the subtree rooted with target node, their depth should be D+k.
After that, you need to find all ancestors of the target node.
For each ancestor, if the depth is d, the distance between this ancestor to the target node is D-d.
So the final step is to find nodes in the other subtree of this ancestor whose distance is k - (D-d).
Problem: I have a binary tree, all leaves are numbered (from left to right, starting from 0) and no connection exists between them.
I want an algorithm that, given two indices (of 2 distinct leaves), visits the tree starting from the greater leaf (the one with the higher index) and gets to the lower one.
The internal nodes of the tree do not contain any useful information.
I should chose the path based only on the leaves indices. The path start from a leaf and terminates on a leaf, and of course I can access a leaf if I know its index (through an array of pointers)
The tree is static, no insertion or deletion of nodes is allowed.
I have developed an algorithm to do it but it really sucks... any ideas?
One option would be to find the least common ancestor of the two nodes, along with the sequence of nodes you should take from each node to get to that ancestor. Here's a sketch of the algorithm:
Starting from each node, walk back up to that node's parent until you reach the root. Count the number of nodes on the path from each node to the root. Let the height of the first node be h1 and the height of the second node be h2.
Let h = min(h1, h2). This is the height of the higher of the two nodes.
Starting from each node, keep following the node's parent pointer until both nodes are at height h. Record the nodes you followed during this step. At this point, both nodes are at the same height.
Until you find a common node, keep marching upwards from each node to its parent. Eventually you will hit their common ancestor. At this point, follow the path from the first node up to this ancestor, then down the path from the ancestor down to the second node.
In the worst case, this takes O(h) time and O(h) space, where h is the height of the tree. For a balanced binary tree is this O(lg n) time and space, which is quite good.
If you're interested in a Much More Hardcore version of this algorithm, consider looking into Tarjan's Least Common Ancestors algorithm, which with linear preprocessing time, can be used to find the least common ancestor much more rapidly than this.
Hope this helps!
Distance between any two nodes can be calculated with the help of lowest common ancestor:
Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca)
where lca is lowest common ancestor.
see this for more help about this algorithm and see this video for learning how to calculate lca.
I'm wondering what the consensus is on the definition of "ancestor" in a computer science context.
I only ask because in Introduction to Algorithms, Second Edition, p. 259 there is a description of the algorithm Tree-Successor(x) that seems odd. In finding the successor of node x,
[...] if the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x.
In a binary search tree with a root having key 2 and children 1 and 3, the successor of 1 is its parent 2. In this case, x is the left child of x's successor, y. According to the book's definition, then, x must be its own ancestor, unless I'm missing something.
I haven't found anything in the errata about this.
It's merely a matter of definition, but in this case, yes. CLRS define an ancestor of x as any node on the unique path from the root to x, which by definition includes x.
The sentence fragment you quoted begins by mentioning exercise 12.2-6 on the next page, which specifies this:
(Recall that every node is its own ancestor.)
:-)
Is a node in a tree considered its own ancestor?
Not normally, AFAIK. For example, in the Wikipedia page on binary trees, ancestor is defined thus:
If a path exists from node p to node q, where node p is closer to the root node than q, then p is an ancestor of q and q is a descendant of p.
But apparently that text book's definition of ancestor is such that a node is its own ancestor. This definition is not exactly intuitive, but a textbook is free to introduce its own definitions for the terminology that it uses. Maybe this definition simplifies some of the related descriptions / theorems / etc.
No, a node is not ancestor of itself. According to me it should be: if the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is either x or an ancestor of x. but the code given in the book supposedly handling such type of cases.