代码改变世界

LeetCode:Flatten Binary Tree to Linked List

2015-08-10 21:59  F_Code  阅读(117)  评论(0编辑  收藏  举报
    //LeetCode:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
    //Flatten Binary Tree to Linked List 114
    //前序遍历,last是链表最后一个节点,然后使用前序遍历,遍历一个节点就在last插入这个节点
    //在遍历的过程中树的结构会被打乱,所以中间变量也就是left right会被保存下来
    class FlattenBinaryTreetoLinkedList
    {
        private TreeNode last = null;
        public void Flatten(TreeNode root)
        {
            if (root == null)
                return;
            if (last != null)
            {
                last.left = null;
                last.right = root;
            }
            last = root;
            TreeNode left = root.left;
            TreeNode right = root.right;
            if (left != null)
            {
                Flatten(left);
            }
            if (right != null)
            {
                Flatten(right);
            }
        }
    }