Leetcode:binary Tree Upside Down

Source: Internet
Author: User

New Root. For Example:given a binary tree {1,2,3,4,5},    1   /   2   3/4   5  Return The root of the binary tree [4,5,2,#,#,3,1].    4  /  5   2    /    3   1  

At first glance, the problem seems to be out of the question, do not know how to flip upside down and left. But drawing a few examples on paper is clear. All right subtrees are either empty or leaf nodes and have left dial hand trees.

So the original number along the Zuozi go down the leftmost node is the root node of the new tree.

The key to this problem is to think of using recursion to do! The structure of this tree, the relationship between the parent-child two-layer node is mostly done by recursion. This is the general direction. The first thing I thought about using stacks, using HashMap to save and using iterations, was a bit complicated. Recursion is simple.

Once the recursion is determined, the problem is solved, that is, go left to the leaf node, return the point as a new root node newroot, define Newroot.left, Newroot.right, and then return Newroot.right as the newroot of the previous layer.

Note that the recursive function eventually returns the node that is not what we asked for, such as returning 1 nodes, and we need the root node of the new tree: node 4. So in the recursive Riga a argument,arraylist<treenode>, to wrap the new root node when it is found. This is a Java function parameter transfer problem, if the direct use of TreeNode as argument, the reference is passed, the function refers to the address of the change, will not let the outside of the reference refers to the address of the change

1  Public classSolution {2      PublicTreeNode upsidedownbinarytree (TreeNode root) {3         if(Root = =NULL)return NULL;4arraylist<treenode> res =NewArraylist<treenode>();5Res.add (NULL);6 Helper (root, res);7         returnRes.get (0);8     }9     Ten      PublicTreeNode Helper (TreeNode root, arraylist<treenode>Res) { One         if(Root.left = =NULL) { ARes.set (0, root); -             returnRoot; -         } theTreeNode Newroot =Helper (Root.left, res); -Newroot.left =Root.right; -Newroot.right =Root; -         returnNewroot.right; +     } -}

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.