题目描述
二叉树数据结构TreeNode可用来表示单向链表(其中left置空,right为下一个链表节点)。实现一个方法,把二叉搜索树转换为单向链表,要求值的顺序保持不变,转换操作应是原址的,也就是在原始的二叉搜索树上直接修改。
返回转换后的单向链表的头节点。
注意:本题相对原题稍作改动
示例:
输入: [4,2,5,1,3,null,6,0] 输出: [0,null,1,null,2,null,3,null,4,null,5,null,6]
提示:
- 节点数量不会超过 100000。
解法
递归将左子树、右子树转换为左、右链表 left 和 right。然后将左链表 left 的最后一个结点的 right 指针指向 root,root 的 right 指针指向右链表 right,并将 root 的 left 指针值为空。
同 897. 递增顺序查找树。
Python3
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def convertBiNode(self, root: TreeNode) -> TreeNode: if root is None: return None left = self.convertBiNode(root.left) right = self.convertBiNode(root.right) if left is None: root.right = right return root res = left while left and left.right: left = left.right left.right = root root.right = right root.left = None return res
Java
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode convertBiNode(TreeNode root) { if (root == null) return null; TreeNode left = convertBiNode(root.left); TreeNode right = convertBiNode(root.right); if (left == null) { root.right = right; return root; } TreeNode res = left; while (left != null && left.right != null) { left = left.right; } left.right = root; root.right = right; root.left = null; return res; } }