美文网首页
2021-07-19 二叉搜索树和双向链表

2021-07-19 二叉搜索树和双向链表

作者: hlchengzi | 来源:发表于2021-07-19 23:06 被阅读0次

    二叉搜索树与双向链表

    public TreeNode Convert(TreeNode pRootOfTree) {
            if(null == pRootOfTree){
                return null;
            }
            //使用二叉树中序遍历,然后修改指针
            ArrayList<TreeNode> list = new ArrayList();
            bianLi(pRootOfTree,list);
            for(int i = 0; i < list.size() - 1; i++){
                list.get(i).right = list.get(i + 1);
                list.get(i + 1).left = list.get(i);
            }
            return list.get(0);
        }
    
        public void bianLi(TreeNode pRootOfTree,ArrayList<TreeNode> list){
             if(null == pRootOfTree){
                 return;
             }
            if(null !=pRootOfTree.left){
                bianLi(pRootOfTree.left,list);
            }
            list.add(pRootOfTree);
            if(null != pRootOfTree.right){
                bianLi(pRootOfTree.right,list);
            }
        }
    

    https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tags=&title=&difficulty=0&judgeStatus=0&rp=1

    不开心的一天,又是这绩效,领导拿你唯唯诺诺。

    相关文章

      网友评论

          本文标题:2021-07-19 二叉搜索树和双向链表

          本文链接:https://www.haomeiwen.com/subject/dkekmltx.html