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:对于某个节点而言,我们每次访问,我们要保留它的左节点,因为它的左节点会变成下一个访问的节点,要保留它的右节点,因为右节点会变成下一个访问节点的兄弟节点,要保留这个节点本身,因为这个节点会变成下一个节点的父节点。
循环遍历左节点,保留相应节点,变化左右指针即可。
原文链接http://blog.youkuaiyun.com/crazy__chen/article/details/47208215
本文介绍了一种特殊的二叉树翻转算法,该算法针对一种特定形态的二叉树,即所有右节点均为叶子节点或者为空的情况。通过迭代方式重新连接节点,实现了将原始树翻转为新的树结构。

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



