首页 > 其他 > 详细

Flatten Binary Tree to Linked List

时间:2015-03-29 12:05:29      阅读:211      评论:0      收藏:0      [点我收藏+]

Flatten Binary Tree to Linked List

问题:

Given a binary tree, flatten it to a linked list in-place.

思路:

  前序pre-order遍历+prenode

我的代码:

技术分享
public class Solution {
    public void flatten(TreeNode root) {
        if(root == null)    return;
        if(pre != null)
        {
            pre.left = null;
            pre.right = root;
        }
        pre = root;
        TreeNode left = root.left;
        TreeNode right = root.right;
        flatten(left);
        flatten(right);
    }
    TreeNode pre = null;
}
View Code

学习之处:

  • 转换成常见的前序遍历问题,然后再加上一个prenode指针就齐活了。

Flatten Binary Tree to Linked List

原文:http://www.cnblogs.com/sunshisonghit/p/4375440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!