Created
August 15, 2015 15:13
-
-
Save xynophon/8272a92bd0b8a45b40e6 to your computer and use it in GitHub Desktop.
LeetCode BinaryTreeUpsideDown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class BinaryTreeUpsideDown { | |
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| public TreeNode upsideDownBinaryTree(TreeNode root){ | |
| TreeNode cur = root; | |
| TreeNode new_left = null; | |
| TreeNode new_right = null; | |
| TreeNode temp = null; | |
| while(cur!= null){ | |
| temp = cur.left; | |
| cur.left = new_left; | |
| new_left = cur.right; | |
| cur.right = new_right; | |
| new_right = cur; | |
| cur = temp; | |
| } | |
| return new_right; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment