【Leetcode】【Medium】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
解题思路:
看上去很简单,使用二叉树的中序遍历就可以实现。但是题目的小曲点在于要在原tree上修改,如果将左儿子放在右儿子位置上,会丢失右子树,导致遍历失败。
解决方法有两种:
1、不使用多余空间,将右子树放在左子树中,最右边儿子的右儿子位置上,也正好满足了,leftchild -> data -> rightchild的中序顺序,但是缺点是要遍历多次树,时间复杂度高;
2、只关注左儿子,将左儿子放到右儿子的位置上,同时使用栈结构,将所有右子树挨个入栈。在没有左儿子时,取出栈中元素。
第二种解法的代码:
/**
* 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) {
stack<TreeNode*> rights;
TreeNode* node = root; while (node != NULL) {
while (node->left) {
if (node->right)
rights.push(node->right);
node->right = node->left;
node->left = NULL;
node = node->right;
} if (node->right) {
node = node->right;
continue;
} if (!rights.empty()) {
node->right = rights.top();
node = node->right;
rights.pop();
} else {
break;
}
} return;
}
};
【Leetcode】【Medium】Flatten Binary Tree to Linked List的更多相关文章
- 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】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】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 ...
- 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-place. For ex ...
- [LeetCode]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 e ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
- 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 ...
- [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 ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
随机推荐
- cmake 简学
https://www.cnblogs.com/cv-pr/p/6206921.html
- Java获取永久图文素材中的网页端Url
package com.epalmpay.test; import com.alibaba.fastjson.JSON;import com.epalmpay.util.HttpClientUtil; ...
- Express4.10.2开发框架中默认app.js的代码注释
//通过require()加载了express.path等模块var express = require('express');var path = require('path');var favic ...
- pycurl安装问题
pycurl安装问题 之前人写的代码中依赖pycurl,所以准备在ubuntu14.04.4 LTS系统上安装一下.发现了不少问题. Could not run curl-config 最开始遇到问题 ...
- uwsgi错误invalid request block size
uwsgi错误invalid request block size 今天使用uwsgi启动django代码,然后打开浏览器输入http://localhost:8000/admin.后台出现下面错误 ...
- PHP多维数据排序(不区分大小字母)
1. PHP中最普通的数组排序方法 sort(); 看个例子: <?php $test = array(); $test[] = 'ABCD'; $test[] = 'aaaa'; $test[ ...
- Windows命令计算MD5与SHA1/256值
certutil -hashfile file MD5 certutil -hashfile file SHA1 certutil -hashfile file SHA256 示例如下:
- 常用工具说明--搭建基于rietveld的CodeReview平台(未测试)
为什么要codereview . 整个团队的编码风格是统一的. . 有高手能对自己的代码指点一二,从而提高编码水平. . 减少低级错误的出现 . 约束自己写高质量的代码,因为是要给人看的. 我们对co ...
- access 2010,语文
access 2010*(报表) 使用报表创建:打开需要创建图形的报表----创建----报表----完成. 使用报表向导创建:创建----报表向导----选择表/查询----选择字段----设置分布 ...
- C++运行符重载、友元函数
Complex.h #pragma once #include <iostream> using namespace std; //表示一个复数 class Complex { priva ...