题目
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

这是一个关于LeetCode第156题的解析,涉及将具有特定结构的二叉树上下翻转。文章介绍了如何将原始的右节点转换为左叶子节点,并提供了两种解决方案,包括迭代和递归的方法。
最低0.47元/天 解锁文章
798

被折叠的 条评论
为什么被折叠?



