LeetCode OJ: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
也就是说用先序遍历的方式将所有的节点都放到右侧来,我这里的方法的主要思想就是每次左侧存在节点的时候就将其原封不动的搬移到右侧来,代码如下:
/**
* 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) {
while(root){
if(root->left){
TreeNode * leftBegin = root->left;
root->left = NULL;
TreeNode * leftEnd = leftBegin;
while(leftEnd->right)
leftEnd = leftEnd->right;
TreeNode * tmpRight = root->right;
root->right = leftBegin;
leftEnd->right = tmpRight;
}
root = root->right;
}
}
};
用java写了一遍,方法与上面基本相同,代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
while(root != null){
if(root.left != null){
TreeNode tmp = root.right;
root.right = root.left;
TreeNode tmpRight = root.right;
while(tmpRight.right != null)
tmpRight = tmpRight.right;
tmpRight.right = tmp;
root.left = null;
}
root = root.right;
}
}
}
LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)的更多相关文章
- 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 ...
 
随机推荐
- atoi函数的一种实现
			
atoi函数的使用实例:[Ubuntu环境] main.c: #include <stdio.h> #include <stdlib.h> extern int factori ...
 - echarts3.8.4实现城市空气质量(结合百度地图bmap.js,小航哥)
			
(小航哥自己实现的)为了事先地图效果,需要以下准备: 用百度地图作为地图,需要 1.bmap.min.js(下载地址https://github.com/ecomfe/echarts ,GitHub上 ...
 - mysql库安装
			
如果缺少<mysql/mysql.h> 先安装mysql,然后apt-get install libmysqlclient-dev即可
 - Java技术路线
			
1.计算机基础: 1.1数据机构基础: 主要学习: 1.向量,链表,栈,队列和堆,词典.熟悉 2.树,二叉搜索树.熟悉 3.图,有向图,无向图,基本概念 4.二叉搜索A,B,C类熟练,9大排序熟悉. ...
 - QT 数字图像处理 笔记一
			
1.被有符号整数和无符号整数十足的坑了一上午.我在实现图像旋转的时候先把坐标轴中心平移到图像中心:painter.translate(up_x+temp_w,up_y+temp_h);注意这里面各个数 ...
 - EG:nginx反向代理两台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储
			
step1: 三台web服务器环境配置:iptables -F; setenforce 0 关闭防火墙:关闭setlinux step2:三台web服务器 装软件 step3: 主机修改配置文件:vi ...
 - Nginx 常见报错
			
Nginx 常见报错 启动报错:[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 原因:这个是nginx重启时经常遇到 ...
 - html5-entities.js消失问题
			
今天用nuxt做项目时,启动npm run dev,项目正常启动, 可过一会儿再试图启动时却报错: * ./libhtml5-entities.js in ./~/html-entities/inde ...
 - [Android]开源中国源码分析之一---启动界面
			
开源中国android端版本号:2.4 启动界面: 在AndroidManifest.xml中找到程序的入口, <activity android:name=".AppStart&qu ...
 - HTTP与HTTPS有什么区别?
			
HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全,为了保证这些隐私数据能加密传输,于是网景公司设计了SSL(Secure Sockets Layer)协议用 ...