Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
For example:
Given a binary tree {1,2,3,4,5},
return the root of the binary tree [4,5,2,#,#,3,1].
解法1:
用分治的方法思考比较容易, 题目的意思是只有两种情况,一种是没有右子树,但可能有左子树,一种是有右子树但一定也有左子树。
变换之后,right tree变成left, root变成left, 而原来的left也需要进行upside down的变化。最后他的root是最终的root, 而他的最右子树变成了现在root和right的parent
C++
Java