Leetcode 114
/**
* 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;
if(root->left) flatten(root->left);
if(root->right) flatten(root->right);
TreeNode* temp = root->right;
root->right = root->left;
root->left = NULL;
while(root->right) root = root->right;
root->right = temp;
} };
似懂非懂
Leetcode 114的更多相关文章
- [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 ...
- Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 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 ...
- Java实现 LeetCode 114 二叉树展开为链表
114. 二叉树展开为链表 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class S ...
- Leetcode 114, Flatten Binary Tree to Linked List
根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...
- 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)
问题 给出一个二叉树,将其原地平面化为链表. 例如,给出: 1 / \ 2 5 / \ \ 3 4 6 平面化后的树看起来应该是这样: 1 \ 2 \ ...
- [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 ...
随机推荐
- WireShark学习
1.打开wireshark->Capture->Interface->选择你的网卡(选中)->Start 2.OK抓包开始,工具栏上有stop,点击停止抓包 3.过滤,这个你可 ...
- Web负载均衡学习笔记之四层和七层负载均衡的区别
0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...
- Linux 命令安装bin文件
Linux 命令安装bin文件 安装命令: //1,增加文件的可执行权限 chmod a+x jdk-6u30-linux-x64.bin //2,程序即安装在执行命令的文件夹下 ./jdk-6u30 ...
- c++ sleep(windows/linux)
c标准中包含了一个sleep用以实现当前线程暂停执行n毫秒,如下所示: 函数名: sleep 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); ...
- Django国际化和本地化
把django的这篇文档看了一遍,基本弄懂了,讲的也挺详细的 https://docs.djangoproject.com/en/1.6/topics/i18n/ 首先是国际化和本地化概念: 1,国际 ...
- 20145304 Exp6 信息搜集与漏洞扫描
20145304 Exp6 信息搜集与漏洞扫描 实验后回答问题 (1)哪些组织负责DNS,IP的管理. NSI负责Internet顶级域名系统的注册.协调与维护,IAIA负责Internet的地址资源 ...
- 【ssh免登录】设置集群环境ssh免登录步骤
1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...
- 51nod 1183 编辑距离
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183. 题意不再赘述. 分析:大概和LCS差不多的吧 但是我用LCS ...
- neuroph Perceptron Sample
错误: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory ...
- Network Simulator for P4(NSP4) src内容介绍
Structure What's NSP4? src source code introduction What's NSP4? NSP4是一个用于P4的网络仿真工具,旨在简化P4的环境部署和运行,将 ...