博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
阅读量:4656 次
发布时间:2019-06-09

本文共 955 字,大约阅读时间需要 3 分钟。

题目

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
解题思路:利用递归找到倒数第一个父节点。记录下它的右节点,将左边的移到右边。然后再把之前标记的右节点连接上。

代码

public class Solution {    public void flatten(TreeNode root) {        if(root==null) return;        flatten(root.left);        flatten(root.right);        TreeNode temp=root.right;        if(root.left!=null){            root.right=root.left;            root.left=null;                            while(root.right != null){           root=root.right;        }        root.right=temp;                    }            }}
代码下载:

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:

******************************************/

posted on
2017-07-20 14:36 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mthoutai/p/7211115.html

你可能感兴趣的文章
SAE上部署Django (Mac OSX)
查看>>
Windows反复重启的可能的解决办法
查看>>
CheckRefreshed 判断页面刷新
查看>>
NOIP 2000 乘积最大
查看>>
fatal: the remote end hung up unexpectedly
查看>>
Delphi-操作剪贴板
查看>>
hdu 1029
查看>>
Docker 容器的网络连接 & 容器互联
查看>>
吾爱专题脱壳练习----压缩壳练习之三
查看>>
LeetCode -- Palindrome Linked List
查看>>
栈应用——逆波兰式表达式的值
查看>>
vscode 快速生成html
查看>>
div模拟textarea且高度自适应
查看>>
windows下vi/vim编辑器的基本操作
查看>>
负载均衡软件LVS分析二(安装)
查看>>
access INSERT INTO 语句的语法错误
查看>>
JQuery异步提交
查看>>
Python:将数组中的元素导出到变量中 (unpacking)
查看>>
ubuntu16.04安装mysql5.6
查看>>
mysql命令行中执行sql的几种方式总结
查看>>