FG面经Prepare: BST to Double LinkedList
BST to double linkedlist in inorder traversal sequence
Follow Up: 如果这个BST自带prev, next, 给一个value,插进去这个node,并补全left,right,prev,next
class TreeNode {
public int val;
TreeNode left;
TreeNode right;
}
TreeNode newRoot
TreeNode tree2Dll(TreeNode root) {
TreeNode pre = null;
TreeNode newRoot = null;
helper(root, pre);
return newRoot;
}
public void helper(TreeNode cur, TreeNode pre) {
if (cur == null) {
return;
}
helper(cur.left, pre);
cur.left = pre;
if (pre == null) {
newRoot = cur;
}
else pre.right = cur;
pre = cur;
helper(cur.right, pre, newRoot);
}
Follow up:
第一步完成基础上,把这个new value插入BST中,一边插入一边记录沿途的大于value的root node(即走了该root node的left child),作为inorder successor. 找到inorder successor之后,通过其prev指针找到inorder predecessor, 改指针就好了
FG面经Prepare: BST to Double LinkedList的更多相关文章
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
- Leetcode总结之DFS
package DFS; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; impo ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- 科学计算器(JAVA实现)
前记: 大二学 Java 的时候写的,现在贴上来,只为留念. 再翻代码,自己看着都头疼.一重重的 if 嵌套,当时写得费劲,现在看着更费劲. 代码思想: 代码的大致思想是这样: 首先定义一个算式字符串 ...
- Leetcode: Insert Delete GetRandom O(1)
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- 标准C++中的STL容器类简单介绍
SGI -- Silicon Graphics[Computer System] Inc.硅图[计算机系统]公司. STL -- Standard Template Library 标准模板库. ...
- 76. Minimum Window Substring
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- Enhancing the Scalability of Memcached
原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...
- 《STL源码剖析》笔记
STL六大组件 1.容器(container):各种数据结构,如vector,list,deque,set,map等 2.算法(algorithm):各种常用算法如sort,search,copy,e ...
随机推荐
- js中一个对象中遇到一个相同的key所对应的value值相加
如图: 变成: js原生如下: var abc=[ {typeid:1,ade:1}, {typeid:2,ade:1}, {typeid:1,ade:2}, {typeid:1,ade:2}, {t ...
- newinstance和new的区别
newinstance将对象创建分为两个步骤,解耦的手段,先调用class.forName加载类,然后再去实例化它 new可以不用加载,不是一定 newinstance作为依赖注入 https://b ...
- Java eclipse导入外部项目时出错怎么解决
从外部环境import一个项目进入Java工作环境中,若出现以下错误,解决方法如下: 鼠标移上去,会出现以下错误提示: 解决方法: 选中该项目->右键->Build path->Co ...
- VsCode创建第一个vue项目
使用vue-cli快速构建项目 vue-cli 是vue.js的脚手架,用于自动生成vue.js模板工程的. 安装vue-cli之前,需要先安装了vue和webpack · node -v ...
- Windows Server 2012设置VMWare以服务方式启动(注销后也可以运行,开机也可以自动运行)
场景:要用一台服务器(Windows Server 2012),加装了一张双口的千兆网卡,安装一个虚拟机,使用爱快在虚拟机里当做软路由. - 那么问题来了,我这台是只能远程的,如果软路由没启动,怎么办 ...
- 关于在虚拟机上安装ubuntu输入不了中文的问题
打开终端后,无法输入中文,按照网络上的教程 1.安装语言包 System Settings–>Language Support–>Install/Remove Languages 选中ch ...
- Yarn任务提交流程(源码分析)
关键词:yarn rm mapreduce 提交 Based on Hadoop 2.7.1 JobSubmitter addMRFrameworkToDistributedCache(Configu ...
- Nginx配置:nginx如何配置跳转fpm
location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_inf ...
- MYSQL 导入导出数据库文件
一.从数据库导出数据库或表文件: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据mysqldump -h localhost -uroot ...
- Linux命令 file
查看文件类型: