[LeetCode] Three solutions for Binary Tree Upside Down

Source: Internet
Author: User

[LeetCode] Three solutions for Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. return the 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 [, 2, #,#,].
4
/\
5 2
/\
3 1

There is an important limit to this question: no right child of the entire number will regenerate branches, basically in the shape of a comb. For questions about the tree type, you can first think of a recursive idea:After the left subtree is reversed, the left and right child pointers of the original left child point to the original root node and the original right brother node respectively..

public TreeNode UpsideDownBinaryTree(TreeNode root) {if (root == null)return null;TreeNode parent = root, left = root.left, right = root.right;if (left != null) {TreeNode ret = UpsideDownBinaryTree(left);left.left = right;left.right = parent;return ret;}return root;}

The second idea is direct Replace recursion with iterationIt is easy to do, and the efficiency will be higher, because it saves the stack space used by recursion.

public TreeNode UpsideDownBinaryTree(TreeNode root) {TreeNode node = root, parent = null, right = null;while (node != null) {TreeNode left = node.left;node.left = right;right = node.right;node.right = parent;parent = node;node = left;}return parent;}

The third idea is special, Converts subsequent traversal to hierarchical traversal.. Note that Java does not support transferring references to the TreeNode address, so a global variable is created here. In addition, similar to the processing of the linked list, here I get a dummy node to simplify the processing of the root node.

private TreeNode out = null;public TreeNode UpsideDownBinaryTree(TreeNode root) {TreeNode dummy = new TreeNode(0);dummy.left = new TreeNode(0);out = dummy;postorder(root);return dummy.right;}private void postorder(TreeNode root) {if (root == null)return;postorder(root.left);postorder(root.right);if (out.left == null) {out.left = root;out.left.left = null;out.left.right = null;} else if (out.right == null) {out.right = root;out.right.left = null;out.right.right = null;}if (out.left != null && out.right != null)out = out.right;}


Related Article

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.