Leetcode binary tree Upside down

Source: Internet
Author: User

The question is to be charged. can only see the topic online, I wait for the cock Silk also can't oj test. After looking at the Internet, it is not original, other parties have similar problems. Here, for example, on the Careercup

The idea of recursion was first used,

TreeNode *ans; TreeNode*helper156 (TreeNode *root) {        if(!root, left &&!rootRight ) {ans= root;returnRoot; } TreeNode*parent = Upsidedown (rootLeft ); Parent-left = rootRight ; Parentright =Root; Root-left =NULL; Rootright =NULL; returnParent-Right ; } TreeNode*upsidedown (TreeNode *root) {        if(Root) helper156 (root); returnans; }

It's mentioned here that if it's a non-recursive, it's almost reverse Linked list II

TreeNode *UPSIDEDOWN2 (TreeNode *root) {        if(!root, left &&!root, right)returnRoot; TreeNode*parent = null, *parentright = NULL, *p =Root;  while(p) {TreeNode*left = PLeft ; P-left =Parentright; Parentright= P-Right ; Pright =parent; Parent=p; P=Left ; }        returnparent; }

It's nice to not have to hand back the return.

Leetcode binary tree Upside down

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.