AcWing 49. 二叉搜索树与双向链表
原题链接
简单
作者:
bright
,
2019-07-05 16:13:58
,
所有人可见
,
阅读 709
Java 代码
class Solution {
public TreeNode convert(TreeNode root) {
TreeNode[] pLast = new TreeNode[1];
dfs(root, pLast);
TreeNode head = pLast[0];
while (head != null && head.left != null) {
head = head.left;
}
return head;
}
private void dfs(TreeNode root, TreeNode[] pLast) {
if (root == null) {
return;
}
dfs(root.left, pLast);
root.left = pLast[0];
if (pLast[0] != null) {
pLast[0].right = root;
}
pLast[0] = root;
dfs(root.right, pLast);
}
}