题目
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}
,
1
/ \
2 3
/ \
4 5
return the root of the binary tree [4,5,2,#,#,3,1]
.
4
/ \
5 2
/ \
3 1
解答
右结点要么为空,要么是有左兄弟的叶结点。把题目中的树拆成一个个小三角形(父结点、左子结点、右子结点)来分别翻转
方法一:迭代
先把
1
/ \
2 3
变成
2
/ \
3 1
然后把
2
/ \
4 5