lintcode-easy-Flatten Binary Tree to Linked List

Flatten a binary tree to a fake "linked list" in pre-order traversal.

Here we use the right pointer in TreeNode as the nextpointer in ListNode.

Example

              1
                    1          2
    / \             2   5    =>    3
  / \   \           3   4   6          4
                                           5
                                               6

Note

Don‘t forget to mark the left child of each node to null. Or you will get Time Limit Exceeded or Memory Limit Exceeded.

Challenge

Do it in-place without any extra memory.

递归一下,注意如果左子树为null直接flatten右子树然后返回,否则超时

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    public void flatten(TreeNode root) {
        // write your code here
        if(root == null)
            return;

        if(root.left == null && root.right == null)
            return;

        if(root.left != null){
            flatten(root.left);
        }

        if(root.right != null){
            flatten(root.right);
        }

        TreeNode temp = root.right;

        if(root.left != null){
            root.right = root.left;
            root.left = null;
            TreeNode p = root;
            while(p.right != null)
                p = p.right;

        p.right = temp;
        }

        return;
    }
}
时间: 2024-10-30 04:24:42

lintcode-easy-Flatten Binary Tree to Linked List的相关文章

刷题114. Flatten Binary Tree to Linked List

一.题目说明 题目114. Flatten Binary Tree to Linked List,将一个二叉树"原地"压缩为"链表"形态的二叉树.难度为Medium! 二.我的解答 这个题目如果允许使用栈的话Easy,先序遍历二叉树,右子树入栈,左子树入栈.当栈不空的时候,将栈顶元素放到右子树即可. class Solution{ public: void flatten(TreeNode* root){ //先根遍历 if(root==NULL) return;

leetcode dfs Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions 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

【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 之 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 思路:对二叉树进行先序遍历,把左子树为空的节点的左子树指向它的后序节点,然后把所有的右子树指向左子树 struct TreeNode { i

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 @ 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

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: 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][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