Binary Tree Upside Down
上下颠倒一个二叉树. 这个题应该是hard难度, 直接看很难发现规律.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * 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; } } |