2023-9-30 JZ36 二叉搜索树与双向链表

题目链接:二叉搜索树与双向链表

2023-9-30 JZ36 二叉搜索树与双向链表_第1张图片

import java.util.*;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    TreeNode pre = null;
    public TreeNode Convert(TreeNode root) {
        if(root == null) return null;
        dfs(root);
        while(root != null && root.left != null) root = root.left;
        return root;
    }

    public void dfs(TreeNode root) {
        if(root == null) return ;
        dfs(root.left);
        root.left = pre;
        if(pre != null) pre.right = root;
        pre = root;
        dfs(root.right);
    }
}

你可能感兴趣的:(链表,深度优先,数据结构,剑指offer)