题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public TreeNode head = null; public TreeNode end = null; public TreeNode Convert(TreeNode pRootOfTree) { invert(pRootOfTree); return head; } private void invert(TreeNode node) { if (node == null) return; invert(node.left); if (end == null) { head = node; end = node; } else { // 拼接到end的右侧,建立双向关联 end.right = node; node.left = end; end = node; } invert(node.right); } }
解题思想:递归中序遍历
#算法##算法笔记#