Leetcode 之Flatten Binary Tree to Linked List(50)

将左子树接到右子树之前,递归解决
void flatten(TreeNode *root)
{
if (root == nullptr)return; flatten(root->left);
flatten(root->right);
//如果没有左子树,直接返回即可
if (root->left == nullptr)return;
p = root->left;
//寻找左子树的最后一个结点
while (p->right)p = p->right;
//将右结点接在左子树的最后一个结点上
p->right = root->right;
//将左子树移到右子树上
root->right = root->left;
root->left = nullptr;
}
Leetcode 之Flatten Binary Tree to Linked List(50)的更多相关文章
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 【LeetCode】Flatten Binary Tree to Linked List
随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
- [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 ...
- [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 ...
- [Leetcode][JAVA] 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 ...
- 【leetcode】Flatten Binary Tree to Linked List (middle)
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- leetcode 114 Flatten Binary Tree to Linked List ----- java
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [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 ...
- [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 ...
随机推荐
- [NOI2001]炮兵阵地 状压DP
题面: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最多 ...
- BZOJ1911 [Apio2010]特别行动队 【斜率优化】
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 5005 Solved: 2455 [Submit][Sta ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- Rabbitmq----基础使用
---------------rabbitmq的基础使用--------------- 查看消息队列信息 在安装好rabbitmq-server之后,启动服务 使用命令rabbitmqctl list ...
- iOS AFNetworking 2.6.0框架导入报错解决方法
最近手动导入AFNetworking 2.6.0框架时发现Xcode报如下3个错误: 1. Use of undeclared identifier ‘kSecFormatUnknown‘ 2. Us ...
- HDU3068:最长回文(Manacher模板)
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【链表】在O(1)的时间删除链表的节点
/** * 在O(1)的时间删除链表的节点 * * @author * */ public class Solution { public static void deleteNode(Node he ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
- (转)史上最好的Python线程指南
来自AstalWind的好文,彻底认识python线程 http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html . . . . .
- c# txt 文件上传、写入TXT文件、创建图形验证码
asp.net mvc 图片上传 html 在使用包含文件上传控件的表单时,必须使用 enctype="multipart/form-data" 属性 <form encty ...