You are viewing a single comment's thread. Return to all comments →
My code is:
public static Node lca(Node root, int v1, int v2) { List<Node> list = new ArrayList<>(); findNode(root, v1, list); List<Node> list2 = new ArrayList<>(); findNode(root, v2, list2); for (int i = list2.size() - 1; i >= 0; i--) { if (list.contains(list2.get(i))) { return list.get(i); } } return null; } public static void findNode(Node root, int value, List<Node> list) { if (root == null) { return; } else { list.add(root); if (root.data == value) return; if (value < root.data) { findNode(root.left, value, list); } else { findNode(root.right, value, list); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Binary Search Tree : Lowest Common Ancestor
You are viewing a single comment's thread. Return to all comments →
My code is: