[Programming question] The minimum common parent node of Two Binary Tree nodes

Source: Internet
Author: User

75. Minimum common parent node (tree) of Two Binary Tree nodes)
Question: The Node definition of a binary tree is as follows:
Struct treenode
{
Int m_nvalue;
Treenode * m_pleft;
Treenode * m_pright;
};
Input two nodes in the binary tree and output the lowest common parent node among the two nodes.

 

Idea: after the modification, an additional integer N is needed to calibrate the method for sequential traversal.

At first, I wanted to use non-recursion, and the results could not be written... so I had to use recursion ....

/* 75. the minimum common parent node (tree) of the two nodes of the binary tree. The Node definition of the binary tree is as follows: struct treenode {int m_nvalue; treenode * m_pleft; treenode * m_pright ;}; input two nodes in the binary tree and output the lowest common parent node among the two nodes. * // Train of thought: Modify the sequential traversal # include <iostream> # include <stdlib. h ># include <vector> using namespace STD; typedef struct treenode {int m_nvalue; treenode * m_pleft; treenode * m_pright;} treenode; void createtree (treenode * & T) {int data; If (scanf ("% d", & Data )! = 0 & Data! = 0) {T = (treenode *) malloc (sizeof (treenode); t-> m_nvalue = data; t-> m_pleft = NULL; t-> m_pright = NULL; createtree (t-> m_pleft); createtree (t-> m_pright);} return;} treenode * findlowestcoparent (treenode * t, int N1, int N2, int * n) {If (t = NULL) {return NULL;} else {int A = 0; int B = 0; treenode * A = findlowestcoparent (t-> m_pleft, N1, N2, & A); treenode * B = findlowestcoparent (t-> m_pright, N1, N2, & B ); if (t-> m_nvalue = N1 | T-> m_nvalue = n2) {(* n) ++ ;}( * n) ++ = (a + B ); if (A = 2) {return a;} If (B = 2) {return B;} If (* n) = 2) {return t ;}} int main () {treenode * t = NULL; createtree (t); int n = 0; treenode * ans = findlowestcoparent (T, 8, 6, & N); printf ("% d", ANS-> m_nvalue); Return 0 ;}

 

I found the online answer. The specific idea is similar, but no extra integer is needed. However, this type of code defaults to that the two input nodes must have a common parent node, that is, the entered number is correct. If an error occurs in the input of my code, for example, root and 2000000, null is returned, and this type of code returns root.

Source: http://www.cnblogs.com/venow/archive/2012/08/31/2664969.html

# Include "stdafx. H "# include <iostream >#include <fstream >#include <ctime> using namespace STD; struct treenode {int m_nvalue; treenode * m_pleft; treenode * m_pright ;}; // assume that the created binary tree is shown in/*/3/\/3 6/\/8 9 10 11/13/*/void createbitree (treenode * & pnode, fstream & Fin, treenode * & pnodeone, treenode * & pnodetwo) {int dat; Fin> dat; If (DAT = 0) {pnode = NULL ;} else {pnode = new treenode (); pnode-> M_nvalue = dat; If (null = pnodeone &&! (RAND () % 3) {pnodeone = pnode;} If (null = pnodetwo &&! (RAND () % 5) {pnodetwo = pnode;} createbitree (pnode-> m_pleft, FIN, pnodeone, pnodetwo); createbitree (pnode-> m_pright, FIN, pnodeone, pnodetwo) ;}}// find the minimum common parent node treenode * findfirstcommonparentnode (treenode * proot, treenode * pnodeone, treenode * pnodetwo) of the Two Binary Tree nodes {If (null = proot) {return NULL;} If (proot = pnodeone | proot = pnodetwo) {return proot;} treenode * pleft = findfirstcommonparentnode (proot-> m_pleft, pnodeone, pnodetwo ); treenode * pright = findfirstcommonparentnode (proot-> m_pright, pnodeone, pnodetwo); If (null = pleft) // 1. No node is found in the left subtree, the first public parent node must be in the right subtree, and the first node is the lowest common parent node {return pright;} else if (null = pright) // 2. If no node is found in the right subtree, the first common parent node must be in the left subtree and the first node is the lowest common parent node {return pleft ;} else // 3. Found in the left and right subtree of the node respectively, the node must be the first public parent node {return proot ;}} int _ tmain (INT argc, _ tchar * argv []) {srand (unsigned) Time (null); fstream fin ("tree.txt"); treenode * proot = NULL; treenode * pnodeone = NULL; treenode * pnodetwo = NULL; treenode * pparent = NULL; createbitree (proot, FIN, pnodeone, pnodetwo); pparent = findfirstcommonparentnode (proot, pnodeone, pnodetwo ); cout <"the first node is:" <pnodeone-> m_nvalue <Endl; cout <"the second node is:" <pnodetwo-> m_nvalue <Endl; cout <"the first parent node is:" <pparent-> m_nvalue <Endl; cout <Endl; return 0 ;}

 

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.