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