Algorithm analysis
We will analyze the algorithm of O (n) directly.
For example, to find the lowest common ancestor of Node F and Node H, first find the path from the root node A to F, and then find the path of a to H, then the last same node is the lowest common ancestor. A->b->d->f and A->b->e->h, the last node of the same thing B, so the lowest common ancestor is the B-node. The algorithm that asks the root node to the specified node has previously been updated, and the complexity is O (n), so the total time complexity is O (n).
Condition Refinement:
(1) If the tree is a two-fork tree, and is a two-fork sort tree.
In this condition, the lowest common ancestor can be found by using the search function of the two-fork sort tree.
(2) The tree is not a binary sort tree, even the two-fork tree is not, is the ordinary tree.
1, if there is a pointer to the parent node in the tree.
This problem can be transformed into two linked list intersection, to find the first intersection of two list.
2, if there is no pointer to the parent node in the tree.
This problem is a bit of a hassle.
In particular, get the function code from the root node to the specified node:
struct Binarynode
{
char value;
Binarynode *left;
Binarynode *right;
};
To the node to the specified node path:
BOOL Getnodepath (Binarynode *proot,binarynode *pnode,vector<binarynode*> &v)
{
if (PRoot==NULL) return
false;
V.push_back (proot);
if (Proot==pnode) return
true;
BOOL Found=getnodepath (PROOT->LEFT,PNODE,V);
if (!found)
Found=getnodepath (proot->right,pnode,v);
if (!found)
v.pop_back ();
}
Find the lowest common ancestor node:
binarynode* getcommonparent (binarynode *proot,binarynode *pnode1,binarynode *pnode2)
{
if (PRoot==NULL | | Pnode1==null | | Pnode2==null) return
NULL;
Vector<binarynode*> V1,v2;
Getnodepath (PROOT,PNODE1,V1);
Getnodepath (PROOT,PNODE2,V2);
Binarynode *plast=proot;
Vector<binarynode*>::iterator Ite1=v1.begin ();
Vector<binarynode*>::iterator Ite2=v2.begin ();
while (Ite1!=v1.end () && ite2!=v2.end ())
{
if (*ite1==*ite2)
plast=*ite1;
ite1++;
ite2++;
}
return pLast;
}
To see a specific ACM topic
Topic Description:
Given a tree, it also gives two nodes in the tree, and asks for their lowest common ancestor.
Input:
The input may contain more than one test sample.
For each test case, the first behavior of the input is a number n (0<n<1000), which represents the number of test examples.
Each test sample includes two rows, the first behavior a first sequence traversal sequence of a binary tree, in which the left and right subtree is empty, is replaced with 0, among which the number of nodes of the two-node_num<10000 tree is the same.
The values of the two nodes in the second behavior tree are M1 and M2 (0<m1,m2<10000).
Output:
corresponding to each test case,
Outputs the value of the lowest common ancestor node of two nodes in a given tree, and outputs "my God" if two given nodes have no minimum common ancestor.
Sample input:
2
1 2 4 6 0 0 7 0 0 5 8 0 0 9 0 0 3 0 0
6 8
1 2 4 6 0 0 7 0 0 5 8 0 0 9 0 0 3 0 0
6 12
Sample output:
2
My God
Train of thought
The idea that I think about this problem is
(1) The idea of the subsequent traversal, with the stack saved to the path of the lookup point
(2) then the two stack first public node
AC Code
#include <stdio.h> #include <stdlib.h> #define N 7000 typedef struct BTREE {struct
Btree *lchild, *rchild;
int data;
} btree;
typedef struct STACK {int top;
btree* Data[n];
} stack;
Stack *first, *second;
int Oneflag, Secflag;
/** * Constructs a binary tree/void Createbtree (Btree **t) {int data recursively according to the sequence of preceding sequences;
scanf ("%d", &data);
if (data = = 0) {*t = NULL;
else {*t = (Btree *) malloc (sizeof (btree));
(*t)->data = data;
Createbtree (& (*t)->lchild);
Createbtree (& (*t)->rchild); }/** * Subsequent traversal of the binary tree, construction traversal stack/void Posttraverse (Btree *t, stack *s, int srcnum, int *flag) {if (T!= NULL)
{Btree *pre;
Pre = NULL;
S->data[s->top + +] = t;
while (S->top > 0 | | | t) {if (t) {s->data[s->top + +] = t; if (T->data = = Srcnum){*flag = 1;
Break
} t = t->lchild;
else {t = s->data[--s->top];
if (T->rchild = NULL | | t->rchild = = PRE) {pre = T;
t = NULL;
else {s->data[s->top + +] = t;
t = t->rchild; /** * Find two stacks the first common element * * T = O (n) * * */void Stackcommondata
(Stack *f, stack *s)
{int top, data, flag; top = (F->top > S->top)?
s->top:f->top; while (Top > 0) {if (F->data[top-1]->data = = s->data[top-1]->data) {data = F->data
[top-1]->data;
flag = 1;
Break
} else {top-;
} if (flag) {printf ("%d\n", data);
else {printf ("my god\n"); }/** * Clean binary tree */void Cleanbtree (BTREE *t) {if (t) {cleanbtree (t->lchild);
Cleanbtree (T->rchild);
Free (t);
{int main (void) {int n, SF, SE;
Btree *t;
scanf ("%d", &n);
while (n-) {createbtree (&t);
scanf ("%d%d", &SF, &se);
A (Stack *) malloc (sizeof (stack));
first->top = 0;
Oneflag = 0;
Posttraverse (t, a, SF, &oneflag);
Second = (stack *) malloc (sizeof (stack));
second->top = 0;
Secflag = 0;
Posttraverse (t, Second, SE, &secflag);
if (oneflag = 0 | | secflag = 0 | | | first->top = 0 | | second->top = = 0) {printf ("my god\n");
Cleanbtree (t);
Continue
else {stackcommondata (second);
Cleanbtree (t);
} return 0;
}
/**************************************************************
problem:1509
User:wangzhengyi
& nbsp; language:c
result:accepted
time:150 Ms
Memory : 110212 KB
****************************************************************/