4
头图

Binary search tree and doubly linked list

Title description

Enter a binary search tree and convert the binary search tree into a sorted doubly linked list. It is required that no new nodes can be created, and only the point of the node pointer in the tree can be adjusted.

topic link : binary search tree and doubly linked list

Code

/**
 * 标题:二叉搜索树与双向链表
 * 题目描述
 * 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
 * 题目链接:
 * https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&&tqId=11179&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz26 {

    private TreeNode pre = null;
    private TreeNode head = null;

    public TreeNode convert(TreeNode pRootOfTree) {
        inOrder(pRootOfTree);
        return head;
    }

    /**
     * 中序遍历
     *
     * @param node
     */
    private void inOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        node.left = pre;
        if (pre != null) {
            pre.right = node;
        }
        pre = node;
        if (head == null) {
            head = node;
        }
        inOrder(node.right);
    }

    public static void main(String[] args) {

    }
}
[Daily Message] To understand the past without admonition, the one who knows the coming can be pursued. It's not far to be lost, and I feel that today is true but yesterday is not.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!