[leetcode] Binary Tree Upside Down
思路:递归实现,注意翻转的规律,以左子树作为根,找到左子树最右边的节点作为原有right子树和根的父节点。
class Solution { public TreeNode upsideDownBinaryTree(TreeNode root) { if (root ==null || root.left ==null && root.right ==null) return root; TreeNode left = upsideDownBinaryTree(root.left); TreeNode right = upsideDownBinaryTree(root.right); TreeNode leftRightest = left; while(leftRightest.right != null){ leftRightest = leftRightest.right; } leftRightest.left = right; leftRightest.right = root; root.left = null; root.right = null; return left; } }