【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
Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
题解:可以看出flatten以后得到的树其实就是原来树的先序遍历,并且所得到的树的左子都有null,右子都是原树先序遍历时的下一个节点。
利用递归先序遍历树即可,用lastNode保存上一个节点的信息,并且在递归过程中要注意保存根节点的右子,因为在递归遍历左子树的过程中,会把根节点的右子指针重新赋值,右子树会丢失。
代码如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private TreeNode lastNode = null;
public void flatten(TreeNode root) {
if(root == null)
return; if(lastNode != null){
lastNode.left = null;
lastNode.right = root;
} lastNode = root; TreeNode right = root.right;
flatten(root.left);
flatten(right);
}
}
【leetcode刷题笔记】Flatten Binary Tree to Linked List的更多相关文章
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【leetcode刷题笔记】Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 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 ...
- LeetCode(114) Flatten Binary Tree to Linked List
题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...
- 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 e ...
- 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 ...
随机推荐
- iOS - 每隔一段时间,反复执行同一个任务
我们有时候会有这样的需求,当程序处于运行状态,每隔几秒给服务器发送一次请求. 这时我们可以这样处理: UILocalNotification *localNotification = [[UILoca ...
- web 网页截取图片
关于在UIwebview上添加滚动图片 两种滚动手势会混淆,应为webview有webview.scrollview的属性 故参照昨天的随笔 scrollview嵌套解决方案. 本篇随笔主要讲循环使用 ...
- 再遇xdebug坑
xdebug.remote_handler=dbgp xdebug.idekey=PHPSTORM ;开启远程调试 xdebug.remote_enable = On ;远程主机 xdebug.rem ...
- PHP计算多少秒/分/时/天/周/月/年之前 : timeago
function timeago( $ptime ) { $etime = time() - $ptime; if ($etime < 59) return '刚刚'; $interval = ...
- iOS main函数讲解
int main(int argc, char * argv[]) { @autoreleasepool { //四个参数 主要讲解后面两个参数 /* 第三个参数:UIApplication或者其子类 ...
- Linux安装过程记录信息
全新的linux安装完成后,会在root目录下有一下三个文件,记录了Linux的安装细节 anaconda-ks.cfg 以Kickstart配置文件的格式记录安装过程中设置的选项信息 install ...
- ALV 表头 ADD_TEXT
[转自http://lz357502668.blog.163.com/blog/static/16496743201252891452493/] CALL METHOD valid_reference ...
- centOS-64位通过YUM源安装nginx
第一步:在 /etc/yum.repos.d/ 目录下,建立名叫nginx.repo的软件源配置文件. 文件 nginx.repo 的内容是: [nginx] name=nginx re ...
- 求阶乘,输入一个正整数 n,输出n!
#include<stdio.h>int factorial (int n); int main(){ int n; scanf("%d",&n); print ...
- 内存写越界导致破环堆结构引起的崩溃问题定位经验[如报错malloc(): memory corruption或free(): invalid next size]
前段时间开发的一个后端C模块上线后,线上出core,初始时,因为訪问压力不大,所以崩溃是上线3天左右出现的.当时用gdb跟进调用堆栈并检查源代码,发现出core位置的代码沒有啥问题.因为当时开发任务较 ...