Binary tree (----) The distance between any two nodes in a binary tree, recursive and non-recursive

Source: Internet
Author: User

1. Binary Tree definition

typedef struct BTREENODEELEMENT_T_ {    void *data;} btreenodeelement_t;typedef struct Btreenode_t_ {    btreenodeelement_t     *m_pelemt;    struct Btreenode_t_    *m_pleft;    struct btreenode_t_    *m_pright;} btreenode_t;


2, the distance between any two nodes in a binary tree

The closest ancestor node of any two nodes in the binary tree (the nearest Public parent node) is essentially the sum of the distances from the nearest ancestor node to the two nodes after the nearest ancestor node is calculated.

(1) Recursive method

First, the nearest ancestor node is obtained according to the recursive method.

Then according to the recursive way, from the nearest ancestor node through the pre-sequence traversal way to a given node, find the path, while calculating the distance (the distance can be considered as two nodes between the edge can be regarded as Unit 1)


(2) Non-recursive mode

int Getlenbetweennodes (btreenode_t *proot, btreenode_t *pnode1, btreenode_t *pnode2) {if (Proot = NULL | | pNode1 = = N ULL | |    PNode2 = = NULL) return 0;        if (pNod1 = = PNOD2) return 0;    Vector <btreenode_t *> vec1;    Vector <btreenode_t *> vec2;    Stack <btreenode_t *> St;    BOOL FindNod1 = false;    BOOL findNod2 = false;    int len = 0; while (!st.empty () | | Proot! = NULL) {//Pre-order traversal, find the path from the root node to the given node while (proot! = NULL) {if (findNod1 = = Fal                SE) {vec1.push_back (proot);            if (Proot = = pNode1) FindNod1 = true;                if (findNod2 = = False) {//does not find the full path, add node Vec2.push_back (proot);            if (Proot = = pNode2) FindNod2 = true;            } if (findNod1 && findNod2)//Both have been found, exit to find break;            St.push (Proot);        Proot = proot->m_pleft;  } if (!st.empty ()) {          Proot = St.top ();            St.pop ();            Proot = proot->m_pright;            if (findNod1 = = false)//path error, delete node vec1.pop_back ();        if (findNod2 = = False) Vec2.pop_back ();    } if (findNod1 && findNod2)//Both have been found, exit to find break;        } if (FindNod1 && findNod2) {vector <btreenode_t *>:: Iterator Iter1 = Vec1.begin ();        vector< btreenode_t *>:: Iterator iter2 = Vec2.begin ();        btreenode_t *lastcommonparent = NULL;        int commonsize = 0; while (Iter1! = Vec1.end () && iter2! = Vec2.end ()) {//At the same time starting from the root node, traverse two paths, find the lowest ancestor node, and record the length from the root node to the lowest ancestor node if (*i                Ter1 = = *iter2) {lastcommonparent = *iter1;            ++commonsize;        } else break;    Len = vec1.size () + vec2.size ()-2*commonsize;//two path lengths-two common lengths, which is the final distance} vec1.clear ();    Vec2.clear ();    St.clear (); return Len;}




Binary tree (----) The distance between any two nodes in a binary tree, recursive and non-recursive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.