Given That 2 Elements Are Present In The Tree Write A Function #567
Given that 2 elements are present in the tree, write a function to find the LCA(Least Common Ancestor) of the 2 elements.
This multiple choice question (MCQ) is related to the book/course gs gs121 Data Structures and Algorithms. It can also be found in gs gs121 Binary Trees - Binary Search Tree - Quiz No.1.
Given that 2 elements are present in the tree, write a function to find the LCA(Least Common Ancestor) of the 2 elements.
public void lca(Tree root,int n1, int n2) { while (root != NULL) { if (root.data() > n1 && root.data() > n2) root = root.right(); else if (root.data() < n1 && root.data() < n2) root = root.left(); else break; } System.out.println(root.data()); }
public void lca(Tree root,int n1, int n2) { while (root != NULL) { if (root.data() > n1 && root.data() < n2) root = root.left(); else if (root.data() < n1 && root.data() > n2) root = root.right(); else break; } System.out.println(root.data()); }
public void lca(Tree root,int n1, int n2) { while (root != NULL) { if (root.data() > n1 && root.data() > n2) root = root.left(); else if (root.data() < n1 && root.data() < n2) root = root.right(); else break; } System.out.println(root.data()); }
public void lca(Tree root,int n1, int n2) { while (root != NULL) { if (root.data() > n1 && root.data() > n2) root = root.left.left(); else if (root.data() < n1 && root.data() < n2) root = root.right.right(); else break; } System.out.println(root.data()); }
Similar question(s) are as followings:
Online Quizzes of gs121 Data Structures and Algorithms
Binary Trees - Binary Search Tree - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Binary Search Tree - Quiz No.2
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Preorder Traversal - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes