LeetCode OJ-- Flatten Binary Tree to Linked List **
https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地。
类似于先跟遍历的顺序。
定义函数
TreeNode* subflatten(TreeNode *root)
并将返回值设置为,先转换出来链表的最后一个位置。
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;
subflatten(root);
}
TreeNode* subflatten(TreeNode *root)
{
if(root->left == NULL && root->right == NULL)
return root;
if(root->left == NULL && root->right!= NULL)
return subflatten(root->right);
//remember root right for temporary
TreeNode *tempr = new TreeNode();
tempr = root->right;
root->right = root->left;
root->left = NULL;
TreeNode *retNode = subflatten(root->right);
if(tempr == NULL)
return retNode;
retNode->right = tempr;
TreeNode *retNode2 = subflatten(tempr);
return retNode2;
}
};
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 ...
随机推荐
- Python知识点入门笔记——特色数据类型(集合)
集合是一种不重复的无序集 集合用花括号来定义{} 集合和字典一样,里面的顺序是无序的,{1,2,3}和{3,2,1}是相等的 集合的元素不可重复,也就是说{1,2,2,3}是不存在的,应该写为{1,2 ...
- 9、python中的控制流
学习完python的基础与数据后,我们就可以编写一些简单的命令了.但这时我们发现,目前位置写出来的程序都是自上而下顺序地执行的.要想程序改变这种自上而下的流程多一点变化,我们就要学习三种程序中的语句. ...
- 异步消息处理机制,UI更新
UI只能在主线程中完成更新,在子线程中更新UI报错如下 Only the original thread that created a view hierarchy can touch its vie ...
- loj6392 「THUPC2018」密码学第三次小作业 / Rsa
还是挺好做的,\((e_1,e_2)=1 \Rightarrow e_1s+e_2t=0\),\(m \equiv m^1 \equiv m^{e_1s+e_2t} \equiv c_1^s c_2^ ...
- 手机APP设计网
http://hao.xueui.cn/ http://www.25xt.com/
- Python+Selenium基础篇之3-打开和关闭IE/Chrome浏览器
前面文章介绍了,如何调用webdriver接口方法来打开和关闭Firefox浏览器,本文介绍如何打开IE和Chrome浏览器.web项目,需要做兼容性测试,最重要的是浏览器兼容性测试.如果只考虑win ...
- Python学习-day12 Mysql
MYSQ数据库的安装使用 Linux/UNIX上安装Mysql Linux平台上推荐使用RPM包来安装Mysql,MySQL AB提供了以下RPM包的下载地址: MySQL - MySQL服务器.你需 ...
- RESTful-rest_framework视图层-第三篇
图书管理系统: 实现图书接口的增.删.改.查 方式一:普通的方式 views配置: #Book的增.删.改.查接口 class BookSerializer(serializers.ModelSeri ...
- java io 流 输入输出 大牛经典总结
在软件开发中,数据流和数据库操作占据了一个很重要的位置,所以,熟悉操作数据流和数据库,对于每一个开发者来说都是很重要的,今天就来总结一下I/O,数据库操作 一:从数据流开始 首先先有一个结构图看一下整 ...
- s if标签
字符串N一定要用“”双引号包含,从test的包含则用单引号 ‘ ’,如果相反,则不能正确判断该属性是否与该字符串相等. 正确:<s:if test='activityBean.searchFor ...