Find the nearest public parent node in the binary tree. There are two cases: parent pointer and parent pointer.
1. It is relatively simple to have a parent pointer. Calculate the depth of the two nodes and move the depth up to the same depth. Move up at the same time until the two nodes are the same, so that the parent node is found. The time complexity of this algorithm is O (n ). Code implementation:
# Include <iostream> struct node {int data; node * left; node * right; node * parent; node (): Left (null), right (null ), parent (null) {}}; int getdpeth (node * n) // node n to root node depth {int COUNT = 0; while (n) {++ count; N = N-> parent;} return count;} node * findnearestcommonancestor (node * N1, node * N2) {int depth1 = getdpeth (N1 ); int depth1 = getdpeth (N2); // move the same depth while (depth1> dep22.) {n1 = N1-> parent; -- depth1;} while (depth1 <depth 2) {n2 = n2-> parent; -- dep2;}// search for while (N1! = N2) {n1 = N1-> parent; N2 = n2-> parent;} return N1;} int main () {// test node * A [11]; for (INT I = 0; I <11; ++ I) {A [I] = new node (); A [I]-> DATA = I ;} for (INT I = 0; I <5; ++ I) {A [I]-> left = A [I * 2 + 1]; A [I * 2 + 1]-> parent = A [I]; A [I]-> right = A [I * 2 + 2]; A [I * 2 + 2]-> parent = A [I];} node * ancestor = findnearestcommonancestor (A [7], a [6]);}2. There is no parent pointer. This is a bit difficult. First, start from the root node and look down. If the root node is equal to one of the child nodes, the root node is the nearest public parent node. Otherwise, calculate the number of N1 or N2 contained in the left and right subtree. If the left subtree contains N1 and N2, the nearest common parent node is located in the left subtree. If the right subtree contains N1 and N2, the right subtree is located. If each left and right subtree contains one, the recent public parent node is the current node. If the binary tree is balanced, the algorithm complexity is O (logn ). The worst case is that the tree is a linked list, and the algorithm time is O (n ^ 2 ). The idea is clear. You can write the code:
# Include <iostream> struct node {int data; node * left; node * right; node (): Left (null), right (null ){}}; // calculate the number of current nodes including N1 and N2 int countmatch (node * Current, node * N1, node * N2) {If (current = NULL) return 0; int COUNT = countmatch (current-> left, N1, N2) + countmatch (current-> right, N1, N2); If (current = N1 | current = n2) return 1 + count; return count;} node * findlca (node * root, node * N1, node * N2) {If (root = NULL) return NULL; if (root = N1 | root = n2) return root; int COUNT = countmatch (root-> left, N1, N2 ); // The left subtree contains the numbers of N1 and N2. If (COUNT = 1) return root; // one left subtree, the right subtree certainly has an else if (COUNT = 2) // all return findlca (root-> left, N1, N2) in the left subtree ); else // all return findlca (root-> right, N1, N2) in the right subtree;} int main () {// test node * A [11]; for (INT I = 0; I <11; ++ I) {A [I] = new node (); A [I]-> DATA = I ;} for (INT I = 0; I <5; ++ I) {A [I]-> left = A [I * 2 + 1]; A [I]-> right = A [I * 2 + 2];} node * ancestor = findlca (A [0], a [7], A [10]);}
There is also a way to find from bottom up. If N1 or N2 is found, it is passed to its parent node. If none is found at the end, null is returned. If both the left and right sides of the current node return non-null values, the current node is the nearest public parent node. In this way, you only need to traverse it once, and the algorithm time complexity is O (n ).
# Include <iostream> struct node {int data; node * left; node * right; node (): Left (null), right (null ){}}; node * findlca (node * root, node * N1, node * N2) {If (root = NULL) // return NULL is not found; if (root = N1 | root = n2) // find return root; node * l = findlca (root-> left, N1, N2 ); // left subtree node * r = findlca (root-> right, N1, N2); // right subtree // both the left and right subtree of the current node locate N1 and N2, then this node is the IF (L! = NULL & R! = NULL) return root; // otherwise, the node is not null, or both are nullelsereturn l! = NULL? L: R;} int main () {// test node * A [11]; for (INT I = 0; I <11; ++ I) {A [I] = new node (); A [I]-> DATA = I ;}for (INT I = 0; I <5; ++ I) {A [I]-> left = A [I * 2 + 1]; A [I]-> right = A [I * 2 + 2];} node * ancestor = findlca (A [0], a [7], a [10]);}
Source code backup on GitHub: https://github.com/KangRoger/Example/tree/master/LeastCommonAncestor
Recent common parent node of Binary Tree