1. 程式人生 > >LeetCode:M-114. Flatten Binary Tree to Linked List

LeetCode:M-114. 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

1、對於當前訪問的node,假設左右兩子樹均為符合要求的展開連結串列

2、函式helper返回值為當前樹展開後的尾部node

3、根據左右子樹返回的連結串列尾部node,將當前父節點與左右子樹串聯成新的連結串列

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void flatten(TreeNode root) {
        helper(root);
    }
    
    public TreeNode helper(TreeNode node){
        
        if(node==null)
            return null;
        
        if(node.left==null && node.right==null)
            return node;
        
        TreeNode leftTail=null;
        if(node.left!=null){
            leftTail=helper(node.left);
        }
        
        TreeNode rightTail=null;
        if(node.right!=null){
            rightTail=helper(node.right);
        }
        
        if(node.left!=null){
            leftTail.right = node.right;            
            node.right = node.left;
            node.left = null;            
        }
        
        if(rightTail!=null){
            return rightTail;
        }else{
            return leftTail;
        }
    }
}


相關推薦

LeetCodeM-114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \

LeetCode114. Flatten Binary Tree to Linked List(固定二叉樹為連結串列)

Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The f

LeetCode 114. Flatten Binary Tree to Linked List

left end anything 實現 fin 後序遍歷 sel [] you 原題 Given a binary tree, flatten it to a linked list in-place. For example,Given 1

[LeetCode] 114. Flatten Binary Tree to Linked List Java

ini like lin ont == lis example 遞歸 font 題目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1

leetcode--114. Flatten Binary Tree to Linked List

turn spa preorder 先序 reorder 復雜 treenode 時間 lin 1、問題描述 Given a binary tree, flatten it to a linked list in-place. For example,Given

LeetCode 114 Flatten Binary Tree to Linked List

assign discuss may pla bee accepted 寫在前面 lis 數據 寫在前面 斷斷續續刷20多道LeetCode上的題了,之所以從這道題尅是記錄,是終於自己慢慢有思路了,不再參考一些discuss中的思路了,還有,終於一次通過編譯,L

[leetcode]114. Flatten Binary Tree to Linked List將二叉樹展成一個連結串列

Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened

leetcode: 114. Flatten Binary Tree to Linked List [✗]

Difficulty Medium. Problem Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 /

LeetCode114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4

#Leetcode# 114. Flatten Binary Tree to Linked List

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/   Given a binary tree, flatten it to a linked list in-place. For example, given t

JavaScript刷LeetCode -- 114. Flatten Binary Tree to Linked List [Medium]

一、題目   Given a binary tree, flatten it to a linked list in-place.   For example, given the following tree: 1 / \ 2 5 / \ \

LeetCode-面試演算法經典-Java實現】【114-Flatten Binary Tree to Linked List(二叉樹轉單鏈表)】

原題   Given a binary tree, flatten it to a linked list in-place.   For example,   Given

114 Flatten Binary Tree to Linked List 二叉樹轉換鏈表

eno 二叉樹 int 方法 ini rip binary right script 給定一個二叉樹,使用原地算法將它 “壓扁” 成鏈表。示例:給出: 1 / \ 2 5 / \ \ 3 4 6壓扁

114. Flatten Binary Tree to Linked List

組成 空間 str new inf linked root 線索二叉樹 image 一、題目   1、審題   2、分析     給出一棵二叉樹,按照先序遍歷順序組成一棵斜右二叉樹。 二、解答   1、思路:     方法一、        采用一個棧進行先序遍歷,遍

114. Flatten Binary Tree to Linked List - Medium

Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened

114. Flatten Binary Tree to Linked ListTree

連結:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 題目:將二叉樹序列化為連結串列 思路: 先序遍歷,儲存一個全域性的newroot,對於當前節點root: 暫存左右子節點,然後將newroot

114. Flatten Binary Tree to Linked List的C++解法

題目描述:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 這個題的關鍵是找到左右子樹的最右下角的位置,因為這將是下一次接子樹的起點。下面這個方法用了遞迴,假設左右子樹將在helper中接好,並且helper返

114. Flatten Binary Tree to Linked List【Medium】【將給定的二叉樹轉化為“只有右孩子節點”的鏈表(樹)】

bmi 轉化 water public fin linked in-place ould baidu Given a binary tree, flatten it to a linked list in-place. For example, given the

LeetCode 114. 二叉樹展開為鏈表(Flatten Binary Tree to Linked List

tree binary ont 基本思想 oot public ike 返回 上一個 題目描述 給定一個二叉樹,原地將它展開為鏈表。 例如,給定二叉樹 1 / 2 5 / \ 3 4 6 將其展開為: 1 2 3

leetcode 114. 二叉樹展開為連結串列(Flatten Binary Tree to Linked List)

給定一個二叉樹,原地將它展開為連結串列。 例如,給定二叉樹 1 / \ 2 5 / \ \ 3 4 6 將其展開為: 1 \ 2 \ 3 \ 4 \ 5