Solution 1: BST转双向链表

问题描述

将一棵二叉查找树(BST)转为有序的双向链表。

例如,有一颗BST如下:

2

|  \

1    3

转成双向链表为:

1=2=3

解决思路


1. 保持有序:中序遍历;

2. 双向链表:记录链表的头节点,遍历过程中记录前一个节点并且保持双向连接关系。

程序

class TreeNode {
	public int val;
	public TreeNode left;
	public TreeNode right;

	public TreeNode(int _val) {
		val = _val;
	}
}

class BSTToDoublyLinkedList {
	public TreeNode convertToDoublyLinkedList(TreeNode root) {
		if (root == null) {
			return null;
		}

		TreeNode head = new TreeNode(0);
		TreeNode pre = new TreeNode(0);
		convertHelper(root, head, pre);
		return head.left;
	}

	private void convertHelper(TreeNode root, TreeNode head, TreeNode pre) {
		if (root == null) {
			return;
		}
		convertHelper(root.left, head, pre);
		if (head.left == null) {
			head.left = root; // record the head
		}
		if (pre.left == null) {
			pre.left = root;
		} else {
			pre.left.right = root;
			root.left = pre.left;
			pre.left = root; // to next
		}
		convertHelper(root.right, head, pre);
	}
}

  

附加测试程序:

public class ConvertTest {
	public static void main(String[] args) {
		TreeNode n2 = new TreeNode(3);
		n2.left = new TreeNode(1);
		n2.left.right = new TreeNode(2);

		BSTToDoublyLinkedList toDoublyLinkedList = new BSTToDoublyLinkedList();
		TreeNode head = toDoublyLinkedList.convertToDoublyLinkedList(n2);
		printDoublyLinkedList(head);
	}

	private static void printDoublyLinkedList(TreeNode head) {
		TreeNode node = head;
		TreeNode last = null;

		System.out.println("left --> right");
		while (node != null) {
			System.out.print(node.val + " ");
			if (node.right == null) {
				last = node;
			}
			node = node.right;
		}

		System.out.println();
		System.out.println("right --> left");

		while (last != null) {
			System.out.print(last.val + " ");
			last = last.left;
		}
	}
}

时间/空间复杂度

时间复杂度:中序遍历的时间复杂度,O(n);

空间复杂度:O(h),递归栈。

时间: 2024-10-14 00:01:59

Solution 1: BST转双向链表的相关文章

笔试算法题(05):转换BST为双向链表 & 查找栈中的最小元素

出题:把二元查找树转变成排序的双向链表.输入一棵二元查找树,要求将该二元查找树按照中序转换成一个排序的双向链表,要求不能创建任何新的节点,只能调整指针的指向: 分析: 递归的思路,当前节点需要进行的处理,并使用递归调用和返回值将子问题链接起来: 首先明白二元查找树的特性,变成有序双向链表后当前根节点的左节点为其原来左子树的最右节点,右节点为其原来右子树的最左节点:因此策略就是针对当前根节点索引的子树,首先判断其为上层节点的右子树还是左子树,从而决定返回最右还是最右节点:然后再递归处理当前根节点的

BST 汇总

参考:http://blog.csdn.net/fightforyourdream/article/details/16843303 import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; /** * http://blog.csdn.net/l

Java 中的引用和指针

在一些编程场景中,我们常常需要记录下某一个特殊的实例位置(如BST转双向链表题中需要记录最终生成的链表的头节点). 在使用Java编程过程中,需要注意引用的问题. class ListNode { public int val; public ListNode next; public ListNode(int _val) { val = _val; } } public class PointerTestInJava { public static void main(String[] arg

链表专题

记录一下<剑指offer>上关于链表的题目 在O(1)时间内删除链表的节点  #13 链表中倒数第k个节点 #15 翻转链表   #16 合并2个排序链表  #17 复杂链表的复制  # 26 BST与双向链表  # 27 两个链表的第一个公共节点 # 37 原文地址:https://www.cnblogs.com/vector11248/p/10503145.html

Solution 9: 判断序列是否为BST的后续遍历结果

问题描述 输入一个整数序列,判断该序列是否为一颗BST的后序遍历序列. 解决思路 递归: (1) 序列的最后一个元素为根节点元素: (2) 在序列中找出前一段代表根节点的左子树孩子们,而剩下的一段为右子树孩子们,检查这些节点的值是否都是大于(等于根节点元素). (3) 然后递归的对两部分进行判断. 程序 public class IsPostorderSequence { public boolean isPostorderSeq(int[] nums) { if (nums == null |

17.13 BST转换成双向链表。

思路:递归执行,子函数需要返回子链表的head和tail,所以借助内部类NodePair来实现. /** * 4 / 2 5 / \ 1 3 6 / 0 0<=>1<=>2<=>3<=>4<=>5<=>6 * */ public class Solution { public TreeNode BSTtoDLL(TreeNode root) { TreeNode res = BSTtoList(root).head; return r

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

[LeetCode] Largest BST Subtree 最大的二分搜索子树

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descendants.Here's an example: 10 / 5 15 / \ \ 1 8 7 The Largest

[LeetCode] Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi