[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
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
题意:将任意一颗二叉树转换成一颗仅有左子树的二叉树。
要求:(1)不得使用额外存储空间,就地排序整理。
(2)修改之后的树为仅有左子树的二叉树,节点顺序为原二叉树的先序遍历。
思路:说是结果为先序遍历,但是递归方式为后序遍历方式。
class Solution {
public:
void flatten(TreeNode *root) {
if(root==NULL) return;
flatten(root->left);
flatten(root->right);
if(root->left==NULL) return;
TreeNode *p = root->left;
while(p->right) p=p->right;
p->right=root->right;
root->right=root->left;
root->left=NULL;
}
};
作者:Double_Win
出处: http://www.cnblogs.com/double-win/p/3875262.html
[LeetCode 题解]: 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 ...
随机推荐
- Python小知识点(3)--装饰器
(1)装饰器含参数,被装饰函数不含(含)参数 实例代码如下: import time # 装饰器函数 def wrapper(func): def done(*args,**kwargs): star ...
- optparse模块
optparse模块主要是用来对参数的记录,相对来说比较灵活, 例子代码如下: #!/usr/bin/env python from optparse import OptionParser usag ...
- Git(二):常用 Git 命令清单
转: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图 ...
- Docker常用操作命令
docker 常用管理命令 修改镜像地址 sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ...
- doStartTag(),doEndTag()方法的执行
1 .TagSupport与BodyTagSupport的区别 TagSupport与BodyTagSupport的区别主要是标签处理类是否需要与标签体交互,如果需要交互的就用TagSupport,否 ...
- php获取远程图片模拟post,file上传到指定服务器
1.获取远程图片 /** $path保存图片的地址 $url要获取的远程图片地址 **/ function getimg($path,$url){ $aext = explode('.', $url) ...
- Linux实战教学笔记47:JAVA企业级应用服务器之TOMCAT实战
第一章 Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache,Sun和其他一些公司及个人共 ...
- 【BZOJ1013】球形空间产生器sphere
高斯消元模板题 #include <cstdio> #include <cstring> #include <algorithm> #include <ios ...
- Mask_RCNN测试自己的模型(练习)
# coding: utf-8 # In[323]: import osimport sysimport randomimport mathimport numpy as npimport skima ...
- 通过snmp监控linux
一.linux snmpd安装 yum install -y net-snmp net-snmp-utils 二.snmp的配置(vim /etc/snmp/snmpd.conf) com2sec n ...