7.15题目总结

 

输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)

递归先序遍历树, 把结点加入路径;若该结点是叶子结点则比较当前路径和是否等于期待和;弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点

    //递归先序遍历树, 把结点加入路径。
    //若该结点是叶子结点则比较当前路径和是否等于期待和。
    //弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点
    private ArrayList<ArrayList<Integer>> listAll = new ArrayList<>();
    private ArrayList<Integer> list = new ArrayList<>();

    public ArrayList<ArrayList<Integer>> FindPath1(Node root, int target) {
        if (root == null) return listAll;
        list.add(root.value);
        target -= root.value;
        if (target == 0 && root.left == null && root.right == null) {
            listAll.add(new ArrayList<>(list));//符合要求的就加入到其中
        }
        FindPath1(root.left, target);
        FindPath1(root.right, target);
        //无论当前路径是否加出了target,必须去掉最后一个,然后返回父节点,
        //去查找另一条路径,最终的path肯定为null
        list.remove(list.size() - 1);
        return listAll;

    }

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

遍历一遍,按链表next顺序将每一个节点依次复制,复制的节点在原节点的next(插入到原链表中);遍历一遍,确定每一个节点的random指针;遍历一遍,断掉复制的连接,生成两个新的链表

关键在于时间复杂度o(n)和空间复杂度o(c)
 

public class CloneComplexLinkedlistTest {
//    输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个
//    节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。
//    (注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
    @Test
    public void test1(){
        RandomListNode node = new RandomListNode(1);
        RandomListNode node1 = new RandomListNode(2);
        RandomListNode node2 = new RandomListNode(3);
        RandomListNode node3 = new RandomListNode(4);
        RandomListNode node4 = new RandomListNode(5);
        RandomListNode node5 = new RandomListNode(6);
        RandomListNode node6 = new RandomListNode(7);
        RandomListNode node7 = new RandomListNode(8);
        node.next =  node1;
        node1.next =  node2;
        node2.next =  node3;
        node3.next =  node4;
        node4.next =  node5;
        node5.next =  node6;
        node6.next =  node7;
        Clone(node);
    }
    public RandomListNode Clone(RandomListNode pHead) {
        clone1(pHead);
        clone2(pHead);
        return clone3(pHead);
    }
    public void clone1(RandomListNode pHead){//遍历一遍
        //按链表next顺序将每一个节点依次复制,复制的节点在原节点的next(插入到原链表中)
        RandomListNode head = pHead;
        while (head != null){
            RandomListNode node = new RandomListNode(pHead.label);
            node.label = head.label;
            node.next = head.next;
            node.random = null;
            head.next = node;

            head = node.next;
        }
    }
    public void clone2(RandomListNode pHead){//遍历一遍,确定每一个节点的random指针
        RandomListNode head = pHead;
        while (head != null){
            RandomListNode node = head.next;
            if(head.random != null){
                node.random = head.random.next;
            }
            head = node.next;
        }
    }
    public RandomListNode clone3(RandomListNode pHead){//遍历一遍,断掉复制的连接,生成两个新的链表
        RandomListNode head = pHead;
        RandomListNode cloneHead = null;
        RandomListNode node = null;
        if(head != null){
            cloneHead = node = head.next;
            head.next = node.next;
            head = head.next;//此时,head为原始head.next,cloneHead为新得到的复制的链表头head
        }
        while (head != null){
            node.next = head.next;
            node = node.next;

            head.next = node.next;//head不为空,则head.next一定不为空(为复制的元素)

            head = head.next;//注意细节
        }
        return cloneHead;
    }
}

class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值