题目:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / 2 5 / \ 3 4 6The flattened tree should look like:
1 2 3 4 5 6
题解:
如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空。
注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子。
整个程序需要维护一个全局变量,保存当前所遍历的节点。
代码如下:
Reference:http://blog.csdn.net/perfect8886/article/details/20000083
此题还有不用递归方法解决的方法,那就是使用栈。
对整棵树一直向右子树方向遍历。当遍历的节点有右孩子时,就将其入栈。有左孩子时,将其更新为当前节点的右孩子,左孩子置空。当左孩子为空时而栈不空时,
就弹出栈,作为右孩子。代码如下:
Reference: //http://www.programcreek.com/2013/01/leetcode-flatten-binary-tree-to-linked-list/
Flatten Binary Tree to Linked List leetcode java,布布扣,bubuko.com
Flatten Binary Tree to Linked List leetcode java
原文:http://www.cnblogs.com/springfor/p/3864355.html