【2021秋冬】【剑指offer】36. 二叉搜索树与双向链表

能想到是中序遍历,但是链表的构造没有想到
题解是类似指针标记

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

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

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
    Node pre,head;
    public Node treeToDoublyList(Node root) {
        if(root == null) return null;
       dfs(root);
       head.left = pre;
       pre.right = head;
       return head;
    }
    void dfs(Node root){
        if(root == null) return ;
        dfs(root.left);
        if(pre == null) head = root;
        else pre.right = root;
        root.left = pre;
        pre = root;
        
        dfs(root.right);

    }
}
上一篇:关于中序线索二叉树网上的一些教程和一些教材代码的问题(java演示)


下一篇:25. K 个一组翻转链表