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 ...
随机推荐
- MySQL通过Navicat实现远程连接的过程
直接使用Navicat通过IP连接会报各种错误,例如:Error 1130: Host '192.168.1.80' is not allowed to connect to this MySQL ...
- 配置ubuntu的超管账号密码
1 在搭建ubuntu 之后 只有自己的账号 2 root 用户是没有配置好的 需要重新配置 3 passwd root 4 输入新密码即可
- loadrunner之java user脚本开发
脚本开发环境: loadrunner11.0 jdk1.6.32_x86_32 脚本开发 1.选择JavaVuser协议 2.配置java环境(Vuser--RunTime Settings) 3.开 ...
- PBRT笔记(2)——BVH
BVH 构建BVH树分三步: 计算每个图元的边界信息并且存储在数组中 使用指定的方法构建树 优化树,使得树更加紧凑 //BVH边界信息,存储了图元号,包围盒以及中心点 struct BVHPrimit ...
- Nested Dolls 贪心 + dp
G: Nested Dolls Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 99 Solved: 19 Descript ...
- Linux bash基础特性一
命令别名 alias cdnet=”cd /etc/sysconfig/network-scripts” 针对用户的别名: “~/.bashrc” 针对系统的别名:”/etc/bashrc” 重读配置 ...
- __x__(6)0905第二天__标签属性=“值”
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- JS的javascript:void(0)用法
javascript:void(0)用法如下: <a href="javascript:void(0)"></a> // 执行js函数,0表示不执行函数. ...
- Linux系统安装tomcat
1.首先下载tomcat:http://tomcat.apache.org/download-60.cgi 2.解压缩tar.gz文件: tar -xzvf xxxxxxx/apache-tomcat ...
- Windows 主机名映射地址
在开发中大数据集群中我们自己的电脑主机名映射不到集群的主机名下面我们就去修改自己电脑 主机名映射地址 c/Windows/System32/drivers/etc/host 文件将主机名和IP地址 ...