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

法I:递归,前序遍历

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
if(!root) return;
else preOrderTraverse(root);
} TreeNode* preOrderTraverse(TreeNode* root){
TreeNode* rightHead = root->right; //save root->right
TreeNode* tail; if(!root->left && !rightHead){
return root;
}
if(root->left) { //have left child
root->right = root->left;
root->left = NULL;
tail = preOrderTraverse(root->right);
if(rightHead){ //have both left child and right child
tail->right = rightHead; //如果left==NULL, 这里就不能使用tail->right,所以得分开讨论有右子树情况
tail = preOrderTraverse(rightHead);
}
}
else{ //only have right child
tail = preOrderTraverse(rightHead);
} return tail;
}
};

法II:迭代

每次循环,找到左子树前序遍历的最后一个节点(即最右的叶子节点),把右节点作为它的右儿子,当前节点的右儿子置为左节点,左节点置为NULL。

然后把当前节点挪到它的右儿子,进入下一次循环

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode *root) {
if (root == NULL) return;
TreeNode *cur = root, *tail = NULL;
while (cur != NULL) {
if (cur->left != NULL) {
tail = cur->left;
while (tail->right) tail = tail->right;
tail->right = cur->right;
cur->right = cur->left;
cur->left = NULL;
}
cur = cur->right;
}
}
};

114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)的更多相关文章

  1. 114 Flatten Binary Tree to Linked List [Python]

    114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...

  2. [LeetCode] 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 T ...

  3. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

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

  4. 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)

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

  5. 【LeetCode】114. 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 ex ...

  6. [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 ...

  7. [LC] 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 ...

  8. LeetCode OJ 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 T ...

  9. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

随机推荐

  1. 制作Windows XP万能克隆镜像

    制作Windows XP万能克隆镜像 战前分析:对于Windows XP,制作万能克隆时的一个重要问题就是系统激活,因为Windows XP为了防止盗版,采取了激活机制,当硬件发生改变时,系统就会要求 ...

  2. smarty基本用法,循环,判断

    require './smarty/Smarty.class.php'; $sm = new Smarty; $sm->setTemplateDir("./dir");//设 ...

  3. RabbitMQ概念

    RabbitMQ 即一个消息队列,_主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用._RabbitMQ使用的是AMQP协议,它是一种二进制协议.默认启动端口 5672. 在 ...

  4. Spark的CombineByKey

    combineBykey关键是要明白里面的三个函数: 1. 当某个key第一次出现的时候,走的是第一个函数(createCombin):A function that creates a combin ...

  5. 关于如何利用Pocket CHM Pro制作帮助文档

    关于如何利用Pocket CHM Pro制作帮助文档 编写人:CC阿爸 2015-4-6 今天在这里,我想与大家一起分享如何利用Pocket CHM Pro制作软件系统的帮助文档,在此做个小结,以供参 ...

  6. linux文本处理命令 一

    1,cut 主要的用途在于将同一行里面的数据进行分解 cut -d ‘分隔符’ -f   ‘第几段’   和-f同时使用 -c    字符区间  截取字符区间 2,grep   cut 是在一行讯息当 ...

  7. (转)SQL server 2005查询数据库表的数量和表的数据量

    本文转载自:http://hi.baidu.com/ajyajyajy/item/4e2a7f4dc83393d2c1a592c1 use DBNAMEgoselect * from sysobjec ...

  8. 详细讲解删除SQL Server日志的具体方法

    一: 删除LOG 1:分离数据库 企业管理器->服务器->数据库->右键->分离数据库 2:删除LOG文件 3:附加数据库 企业管理器->服务器->数据库-> ...

  9. struts2学习(7)值栈简介与OGNL引入

    一.值栈简介: 二.OGNL引入: com.cy.action.HelloAction.java: package com.cy.action; import java.util.Map; impor ...

  10. jquery发送数组

    假设keywordid_list是一个javascript数组,将它发送到keywordbatchsetting.php. $.ajax({ url:"keywordbatchsetting ...