Binary Tree Upside Down
上下颠倒一个二叉树. 这个题应该是hard难度, 直接看很难发现规律.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null)
return root;
if(root.left == null)
return root;
TreeNode r = upsideDownBinaryTree(root.left); // go left
root.left.left = root.right;
root.left.right = root;
root.left = null;
root.right = null;
return r;
}
}