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 ...
随机推荐
- numpy的基础运算2-【老鱼学numpy】
numpy的基础运算中还有很多运算,我们这里再记录一些. 最小/大值索引 前面一篇博文中我们讲述过如何获得数组中的最小值,这里我们获得最小/大值的索引值,也就是这个最小/大值在整个数组中位于第几位. ...
- 昨天开始使用lr controller 已停止工作问题
其实看到这个,只能看日志 看到日志也是无能为力 然后只能尝试修复,但是无法解决,最后通过重装系统,问题解决
- Android-Layer list
Android-Layer list 学习自: KEEGAN小钢 原文链接 : (https://keeganlee.me/post/android/20150909) 使用layer-list 可以 ...
- LCA的在线与离线算法
在线:链接 离线:链接
- 移除K位数字
1.题目来源:选自LeetCode 402: 2.问题描述: 3.问题分析 通过分析我们可以得出这样的结论:如果后一个数字比前面的数字小的话,那么我们就要把前面的一个数字删除掉,并且每次把字符串中拆出 ...
- myeclipse 无法启动Tomcat(程序未设置断点)This kind of launch is configured to open the Debug perspective ...
myeclipse 中在新建一个项目之后想要运行一下,可是却提示This kind of launch is configured to open the Debug perspective,下面是我 ...
- poj2976 Dropping tests(01分数规划 好题)
https://vjudge.net/problem/POJ-2976 又是一波c++AC,g++WA的题.. 先推导公式:由题意得 Σa[i]/Σb[i]<=x,二分求最大x.化简为Σ(a[i ...
- [LeetCode] Expressive Words 富于表现力的单词
Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...
- DEV_TreeList使用经验小结
1. 点击叶子节点是希望Open键显示,点击非叶子节点时希望隐藏.实践中发现点击到了非叶子节点图标,Open没有隐藏,如何解决? 增加一个判断: if (_hitInfo.HitInfoType != ...
- 【Java算法學習】斐波那契數列問題-兔子產子經典問題
/** * 用遞推算法求解斐波那契數列:Fn = Fn-2 +Fn-1; */ import java.util.*; public class Fibonacci { public static v ...