题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

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);
    }
}

全部评论

相关推荐

WesterlyDrift:你拍完照又把选项改回去的样子真的很狼狈😤😤
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务