LeetCode 897. 递增顺序查找树
题目链接
897. 递增顺序查找树 easy
题目思路
这个题有点像二叉树变单链表,利用中序遍历去进行更改即可。
代码实现
class Solution {
TreeNode head;
TreeNode pre;
public TreeNode increasingBST(TreeNode root) {
helper(root);
return head;
}
public void helper(TreeNode root){
if(root == null){
return;
}
helper(root.left);
if(head == null){
head = root;
}else{
pre.left = null;
pre.right = root;
}
pre = root;
helper(root.right);
}
}