Created
September 2, 2017 23:26
-
-
Save cixuuz/3c3a0d161eecbd19aef0b39111a1958b to your computer and use it in GitHub Desktop.
[156. Binary Tree Upside Down] #leetcode
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
class Solution { | |
public TreeNode upsideDownBinaryTree(TreeNode root) { | |
if (root == null) return null; | |
TreeNode node = root.left; | |
TreeNode right = root.right; | |
TreeNode preNode = root; | |
root.left = null; | |
root.right = null; | |
while (node != null) { | |
TreeNode leftLeft = node.left; | |
TreeNode leftRight = node.right; | |
node.left = right; | |
node.right = preNode; | |
right = leftRight; | |
preNode = node; | |
node = leftLeft; | |
} | |
return preNode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment