LeetCode——Flatten Binary Tree to Linked List

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

Hints:

If you notice carefully in the flattened tree, each node‘s right child points to the next node of a pre-order traversal.

原题链接:https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/

题目:给定二叉树,按前序位置展平成一个链表。

思路:递归处理。把右子树放到左子树之后,并清空左子树。

	public void flatten(TreeNode root) {
		if(root == null)
			return;
		flatten(root.left);
		flatten(root.right);
		TreeNode tmp = root;
		if(tmp.left == null)
			return;
		else
			tmp = tmp.left;
		while(tmp.right != null)
			tmp = tmp.right;
		tmp.right = root.right;
		root.right = root.left;
		root.left = null;
	}
    // Definition for binary tree
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

同样的做法,但是非递归。

	public void flatten(TreeNode root){
		while(root != null){
			if(root.left != null){
				TreeNode tmp = root.left;
				while(tmp.right != null)
					tmp = tmp.right;
				tmp.right = root.right;
				root.right = root.left;
				root.left = null;
			}
			root = root.right;
		}
	}

reference : http://blog.csdn.net/perfect8886/article/details/20000083

时间: 2024-08-05 04:15:16

LeetCode——Flatten Binary Tree to Linked List的相关文章

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: 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 6Hints: If you notice carefully

LeetCode: Flatten Binary Tree to Linked List [114]

[题目] 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. [题意] 将一颗二叉树转化为链表,right充当next指针,元素顺序为先序遍历的循序.不使用额外的空间 [思路] 先对左子树链表化,然后将右子树链表化.然后

Leetcode:Flatten Binary Tree to Linked List 解题报告

Flatten Binary Tree to Linked List 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 SOLUTION 1:使用递归解决,根据left是否为空,先连接left tree, 然后再连接右子树.使用一个tail 来记录链的结尾.在递

[LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

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

【LeetCode】Flatten Binary Tree to Linked List

题目:Flatten Binary Tree to Linked List <span style="font-size:18px;">/**LeetCode Flatten Binary Tree to Linked List * 题意:给定一个二叉树,将其转变为一个相当于单链表的结构,观察可知该结构即:每一个节点左儿子为空,右儿子指向自己先序遍历时的下一个节点 * 思路:有观察可得,应对其进行先序遍历,得到正确的序列连接起来 * Definition for binar

Flatten Binary Tree to Linked List leetcode java

题目: 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 题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空.注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子.整个程序需要维护一个全局变量,保

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

【leetcode刷题笔记】Flatten Binary Tree to Linked List

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 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

[Leetcode][Tree][Flatten Binary Tree to Linked List ]

按照前序遍历的顺序把树用right连起来. 本来想了半天,一点思路都没有,总觉得Inplace的解法一般都非常巧妙. 后来我突发灵感,决定用一个变量记录当前访问到哪个点,真是太机智了~~ 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), r