码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Flatten Binary Tree to Linked List

时间:2015-08-10 23:39:02      阅读:170      评论:0      收藏: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);
            }
        }
    }

LeetCode:Flatten Binary Tree to Linked List

标签:

原文地址:http://www.cnblogs.com/FJuly/p/4719378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!