题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
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 { public TreeNode Convert(TreeNode pRootOfTree) { if(pRootOfTree == null) return null; ArrayList<Integer> preList = new ArrayList<>(); pre(pRootOfTree, preList); TreeNode root = new TreeNode(preList.get(0)); TreeNode preNode = root; for(int i = 1; i < preList.size(); i ++){ TreeNode currentNode = new TreeNode(preList.get(i)); preNode.right = currentNode; currentNode.left = preNode; preNode = currentNode; } return root; } void pre(TreeNode root, ArrayList<Integer> preList){ if(root == null) return; pre(root.left, preList); preList.add(root.val); pre(root.right, preList); } }