LeetCode114 Flatten Binary Tree to Linked List

详细见:leetcode.com/problems/flatten-binary-tree-to-linked-list


Java Solution: github

package leetcode;


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

	For example,
	Given
	
	         1
	        / \
	       2   5
	      / \   \
	     3   4   6
	The flattened tree should look like:
	   1
	    \
	     2
	      \
	       3
	        \
	         4
	          \
	           5
	            \
	             6
	click to show hints.
	
	Hints:
	If you notice carefully in the flattened tree,
	 each node's right child points to the next node of a pre-order traversal.
 */


import tools.TreeNode辅助.TreeNode;

public class P114_FlattenBinaryTreetoLinkedList {
	static int N = Integer.MIN_VALUE;
	public static void main(String[] args) {
		TreeNode root = tools.TreeNode辅助.A_生成满二叉树(new int[]{
				1,
				2, 3,
				4, 5, 6, 7,
				8, 9, 10, 11, 12, 13, 14, 15,
		});
		tools.TreeNode辅助.B_按层打印(root);
		new Solution3().flatten(root);
		tools.TreeNode辅助.B_按层打印(root);
	}
	//AC 1ms 31.65%
	static class Solution {
	    public void flatten(TreeNode root) {
	        pre_reverse_order(root, new TreeNode[] {null});
	    }
	    void pre_reverse_order(TreeNode root, TreeNode[] pre) {
	        if (root == null) return;
	        pre_reverse_order(root.right, pre);
	        pre_reverse_order(root.left, pre);
	        root.right = pre[0];
	        root.left = null;
	        pre[0] = root;
	    }
	}
	//AC 1ms 31.65%
	static class Solution2 {
	    public void flatten(TreeNode root) {
	        TreeNode now = root, pre = null;
	        while (now != null) {
	            System.out.println(now);
	            if (now.left != null) {
	                pre = now.left;
	                while(pre.right != null)
	                    pre = pre.right;
	                pre.right = now.right;
	                now.right = now.left;
	                now.left = null;
	            }
	            now = now.right;
	        }
	    }
	}
	static class Solution3 {
	    public void flatten(TreeNode root) {
	        TreeNode n = root, p = null;
	        while (n != null) {
	            System.out.println(n);
	            while (n.left != null) {
	                //
	                p = n.left;
	                while (p.right != null)
	                    p = p.right;
	                p.right = n.right;
	                //set n
	                n.right = n.left;
	                n.left = null;
	            }
	            n = n.right;
	        }
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/flatten-binary-tree-to-linked-list
    search: AC 6ms 12.50%
    solve:  AC 3ms 42.50%
*/

#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

typedef struct TreeNode stn;
typedef struct TreeNode * ptn;

void search(ptn root, ptn* pre) {
    if (root == NULL) return;
    search(root->right, pre);
    search(root->left, pre);
    root->right = *pre;
    *pre = root;
    root->left = NULL;
}

void solve(ptn root) {
    ptn n = root, p = NULL;
    while (n != NULL) {
        if (n->left != NULL) {
            p = n->left;
            while (p->right != NULL)
                p = p->right;
            p->right = n->right;
            n->right = n->left;
            n->left = NULL;
        }
        n = n->right;
    }
}


void flatten(ptn root) {
    //ptn pre = NULL;
    //search(root, &pre);
    solve(root);
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/flatten-binary-tree-to-linked-list
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年4月30日
    @details:    Solution:  55ms 58.71%
    @details:    Solution2: 45ms 96.52%
'''

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def search(self, n, l):
        if n == None: return
        l.append(n)
        self.search(n.left, l)
        self.search(n.right, l)
        
    def flatten(self, n):
        """
        :type n: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        l = []
        self.search(n, l)
        l.append(None)
        for i in range(len(l)-1):
            l[i].left = None
            l[i].right = l[i+1]

class Solution2(object):
    def flatten(self, n):
        r, p = n , None
        while r != None:
            if (r.left != None):
                p = r.left
                while p.right != None:
                    p = p.right
                p.right = r.right
                r.right = r.left
                r.left = None
            r = r.right
                
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值