AcWing 49. 二叉搜索树与双向链表
原题链接
中等
作者:
小轩喵灬
,
2025-01-11 18:03:45
,
所有人可见
,
阅读 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private TreeNode pre = null;
private TreeNode head = null;
public TreeNode convert(TreeNode root) {
inOrder(root);
return head;
}
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);
}
}