Question

In: Computer Science

a.)  Let T be a binary tree with n nodes. Define the lowest common ancestor (LCA) between...

a.)  Let T be a binary tree with n nodes. Define the lowest common ancestor (LCA) between two nodes v and w as the lowest node in T that has both v and w as descendants. Given two nodes v and w, write an efficient algorithm, LCA(v, w), for finding the LCA of v and w. Note: A node is a descendant of itself and v.depth gives a depth of a node v.

b.) What is the running time of your algorithm? Give the asymptotic tight bound (Q) of its running time in terms of n and justify your answer.

Solutions

Expert Solution

I am writing code in c++

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
       if(root==NULL||root==p||root==q)
           return root;
        TreeNode* left=lowestCommonAncestor(root->left,p,q);
        TreeNode*right=lowestCommonAncestor(root->right,p,q);
        if(left&&right)
            return root;
        return left!=NULL?left:right;
      
    }

Above function will return LCA of node p and q in the binary tree with root node as root.

b) Running time of algorithm will be O(n) as in worst case height of binary tree can be atmost n.


Related Solutions

PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor of two nodes in a tree (nodesLCA). The two nodes are input by the user identified by their values. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose.Illustrate the performance of the nodesLCA method. For the BST of datalist excute the method on following pairs: (500, 271), (21, 203) and (53 , 991)...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor...
PYTHON CODING Create a method for a Binary Search tree that finds the lowest common ancestor of two nodes in a tree (nodesLCA). The two nodes are input by the user identified by their values. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify its purpose. Illustrate the performance of the nodesLCA method. For the BST of datalist excute the method on following pairs: (500, 271), (21, 203) and (53 ,...
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
10) A binary tree with N nodes is at least how deep? How deep is it...
10) A binary tree with N nodes is at least how deep? How deep is it at most? 12) A Binary Search Tree is a binary tree with what additional property? 13) Beginning with an empty binary search tree, insert the following values in the order given. Draw the tree at each step of the process. 1 10 5 20 22 7 14) Now delete the value 10 from the tree in question 13. Show each of two possible configurations...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
Draw a simple phylogenetic tree for two sister clades with a common ancestor. Define the following...
Draw a simple phylogenetic tree for two sister clades with a common ancestor. Define the following terms and use them to label your diagram: clade, node, root, and branch.
. Let BT Node be the class we often use for binary-tree nodes. Write the following...
. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children.
2. Let BT Node be the class we often use for binary-tree nodes. Write the following...
2. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children. 3. Suppose you want to improve Merge Sort...
Let T = (V,E) be a tree, and letr, r′ ∈ V be any two nodes....
Let T = (V,E) be a tree, and letr, r′ ∈ V be any two nodes. Prove that the height of the rooted tree (T, r) is at most twice the height of the rooted tree (T, r′).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT