[LeetCode 114] - 将树平面化为链表(Flatten Binary Tree to Linked List)
问题
给出一个二叉树,将其原地平面化为链表。
例如,给出:
1
/ \
2 5
/ \ \
3 4 6
平面化后的树看起来应该是这样:
1
\
2
\
3
\
4
\
5
\
6
初始思路
观察例子中平面化的过程,不难发现其实就是一个二叉树前序遍历的过程。让我们复习一下二叉树前序遍历的方法,根据wiki条目Tree traversal,伪代码如下:
preorder(node)
if node == null then return
visit(node)
preorder(node.left)
preorder(node.right)
iterativePreorder(node)
parentStack = empty stack
while not parentStack.isEmpty() or node != null
if node != null then
visit(node)
parentStack.push(node.right)
node = node.left
else
node = parentStack.pop()
这里我们实现基本照搬以上伪代码就行了,唯一一点小改动是增加了一个TreeNode*变量来记录前序遍历过程中上一个节点。这样我们在依前序遍历到第n个节点时,可以通过这个指针将第n-1个节点的right指向当前节点,left置空。选择非递归方案的代码如下:
class Solution {
public:
void flatten(TreeNode *root)
{
if(!root)
{
return;
} previous_ = nullptr; TreeNode* current = root; while(current || !rightCandidates_.empty())
{
if(!current)
{
current = rightCandidates_.top();
rightCandidates_.pop();
} if(previous_)
{
previous_->right = current;
previous_->left = nullptr;
} previous_ = current; if(current->right)
{
rightCandidates_.push(current->right);
} current = current->left;
}
} private: TreeNode* previous_; std::stack<TreeNode*> rightCandidates_;
};
flatten
Judge Small和Judge Large都顺利通过。
[LeetCode 114] - 将树平面化为链表(Flatten Binary Tree to Linked List)的更多相关文章
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
1. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...
- leetcode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树,原地将它展开为链表. 示例: 给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ ...
- [Swift]LeetCode114. 二叉树展开为链表 | 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(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 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
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- 114 Flatten Binary Tree to Linked List [Python]
114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...
- [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- 【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 ...
随机推荐
- L - Kakuro Extension - HDU 3338 - (最大流)
题意:有一个填数字的游戏,需要你为白色的块内填一些值,不过不能随意填的,是有一些规则的(废话),在空白的上方和作方给出一些值,如果左下角有值说明下面列的和等于这个值,右上角的值等于这行后面的数的和,如 ...
- 数组在C++和java中的区别
几乎所有的程序设计语言都支持数组.在C和C++中使用数组是很危险的.因为C和C++中的数组就是内存块.如果一个程序要访问其自身内存块之外的数组,或者在数组初始化之前使用它,都会产生难以预料的后果. j ...
- windows 编程 之 问题解决笔记
问题目录: 1.如何隐藏和显示窗口 2.InvalidateRect在连续使用鼠标或光标时暂时不起作用 3.在VC项目里自己添加头文件和cpp文件在编译阶段报错 4.在static 控件里添加子控件或 ...
- Sublime Text 2.0.2 注册码
Sublime Text 2.0.2 注册码 直接输入注册码就可以了----- BEGIN LICENSE -----Andrew WeberSingle User LicenseEA7E-85560 ...
- linux 启动network后报错:device eth0 does not seem to be present, delaying initialization
问题背景: 在vsphere client中部署ovf模板后启动linux 的network后提示:device eth0 does not seem to be present, delaying ...
- 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择
一.分析 对于一个字符串进行拼接有三种方法:加号.concat方法.及StringBuiler或StringBuffer. 1."+"方法拼接字符串 str += " ...
- STL之Pairs
什么是Pair 关于类Pair的介绍,下面是引自<C++ Standard Library>的一段话: The class pair is provided to treat two va ...
- 修改MySQL引擎
1. 显示MySQL支持的引擎:show engines;
- Webpack 基本环境搭建
1. 第一步安装之前 先npm init 创建 package.json cnpm init; 然后全局安装 cnpm install webpack -g 确保哪里都可以使用 cnpm instal ...
- activiti总结
1.activiti如何修改登录用户名?在哪个数据库里面添加. 2.activiti的启动和部署在http://activiti.org/userguide/index.html#demo.setup ...