A very important difference between JS, Java brush problem and C brush question

Source: Internet
Author: User

Is recently in the tree aspects of the problem, found that JS and Java brush problems and C brush a very important difference is the difference between the passing of the null when traversing, c parameters can be passed in the null pointer, because recursion in, out, the pointer still points to the place but JS and Java, If the incoming is not a reference, then the previous value is recorded by the return value, otherwise the previous state is lost. I often make mistakes in null here, because NULL is a null pointer in C, NULL is not a reference in JS and Java, only a special value. so when using JS and Java to brush the topic of tree recursion, it is best to pass in the value of your request and then return it, such as a null node. For example, the sword refers to the offer of 26 binary search tree and two-way linked list. We use JS to write in the C language.
function Convert (prootoftree) {   //write code here    if ( Prootoftree = = null) return null;    let plast=null;    Convertnode (prootoftree,plast);    Let phead=plast;    while (phead&&phead.left) {        phead=phead.left;   }    return phead;} function Convertnode (pnode,plast) {    if (pnode==null) return;    if (pnode.left) {        Convertnode (pnode.left,plast);   }    pnode.left=plast;    if (pLast) {         plast.right=pnode;   }    plast=pnode;    if (pnode.right) {& nbsp       Convertnode (pnode.right,plast);   }} 
  But this can't run through, the result is empty, why is there a problem is as follows, when you recursion, to the first layer of plast=null, the second layer plast=null, to the third layer when PLast point to the 4 node, And then go back to the time Plast because the second layer is a null value, that is, from the second layer into the third layer when the pass is not the address (just a null value only), so from the third layer out of the second layer, the previous plast is cleared, then use the second layer of the Plast value , that is, at this point the value of Plast is null: (may be more around the mouth, but you should be able to understand) good slag residue summed up, if you also in multiple languages, I hope you do not trample the same pit with me. The correct approach is as follows
function Convert (prootoftree) {//write code here if (Prootoftree = = null) return null;    Let Plast=null;    Plast=convertnode (Prootoftree,plast);    Let Phead=plast;    while (Phead&&phead.left) {phead=phead.left; } return phead;}    function Convertnode (pnode,plast) {if (pnode==null) return;    if (pnode.left) {plast=convertnode (pnode.left,plast);    } pnode.left=plast;    if (pLast) {plast.right=pnode;    } Plast=pnode;    if (pnode.right) {plast=convertnode (pnode.right,plast); } return pLast;}

A very important difference between JS, Java brush problem and C brush question

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.