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 ...
随机推荐
- easyui提交form表单接受数据处理、
$('#Form').form('submit', { url:"withdrawal/bankAuthenticate4List.do", onSubmit: function( ...
- 03-Python入门学习-Python基础
一.与用户交互 1.什么是与用户交互 程序等待用户输入一些数据,然后程序执行完毕后为用户反馈信息 2.为何程序要与用户交互 为了让计算机能够像人一样与用户的交互 3.如何用 在python3中:inp ...
- JS AJAX 跨域
原因: 浏览器的同源策略,不允许AJAX 访问 其他接口 协议,域名,端口 一个不同 就跨域了 http 端口(80) https(443) 可以跨域的三个标签: 1. img : 打点统计,没有浏 ...
- Java 多线程 sleep()方法与yield()方法的区别
sleep()方法与yield()方法的区别如下: 1 是否考虑线程的优先级不同 sleep()方法给其他线程运行机会时不考虑线程的优先级,也就是说,它会给低优先级的线程运行的机会.而yield()方 ...
- HTML入门8
今天开始接触HTML里面的多媒体和嵌入内容 前面只讲了文字,下面来讲能够让网页动起来,更加有趣的嵌入元素,包含多媒体,包含图像的不同方式,以及怎样嵌入视频. HTML中图片,下面将深入使用它,以及&l ...
- poj1106
极角排序扫一圈. 今天没什么状态写个水题减轻负罪感(大雾) #include <cstdio> #include <cmath> #include <cstring> ...
- IDEA环境下SSM整合------环境配置
声明:本文纯为个人笔记整理,如有不妥之处还望及时指出,欢迎转载! 只为解决操作问题,可以从第二幅图往后看! 一.做不出详细的概念叙述和文本设计,本文主要以实战步骤为主,少量解释为辅助,下面请大家牢记两 ...
- 滑动viewpage
Adapter: package com.example.fashionyuan.Adatader; import android.support.v4.app.Fragment;import and ...
- Retrieve pictures from Excel file using OLEDB
string file = @"C:\sample.xlsx"; if(System.IO.File.Exists(file)) { Microsoft.Office.Intero ...
- EL表达式JSTL
EL表达式语言中定义了11个隐含对象,使用这些隐含对象可以很方便地获取web开发中的一些常见对象,并读取这些对象的数据. 语法:${隐式对象名称}:获得对象的引用 序号 隐含对象名称 描 ...