[编程题]二叉搜索树与双向链表(Java实现)

[编程题]二叉搜索树与双向链表(Java实现) 题目来源

剑指offer第26题
https://www.nowcoder.com/questionTerminal/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 Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null)
            return pRootOfTree;
        TreeNode l=Convert(pRootOfTree.left);
        TreeNode left=l;
        while(left!=nullleft.right!=null){
            left=left.right;
        }
        if(left!=null){
            left.right=pRootOfTree; 
             pRootOfTree.left=left;
        }
        TreeNode right=Convert(pRootOfTree.right);
        while(right!=nullright.left!=null){
            right=right.left;
        }
        if(right!=null){
            pRootOfTree.right=right;
            right.left=pRootOfTree;
        }
        
        return l==null?pRootOfTree:l;
        
    }
}
最新回复(0)
/jishuEl6xfX6ud4GoSQvJcFEYGKNVztq03b9NB_2B8S_2FA_3D_3D4795274
8 简首页