[刷题]剑指offer(java版)26-40

26.[二叉树中和为某一值的路径]

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

思路:用递归,判断当前的和是否等于节点路径以及当前节点是不是目标整数,如果都满足,那么这个list是符合要求的,不然就对左子树或者右子树中递归地找
代码:

public class Solution {
    private ArrayList<ArrayList<Integer>> allList = new ArrayList<ArrayList<Integer>>();
    private ArrayList<Integer> list = new ArrayList<Integer>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        if(root==null)
            return allList;
        list.add(root.val);
        target-=root.val;
        if(target==0&&root.left==null&&root.right==null)
        {
         allList.add(new ArrayList<Integer>(list));
        }
            FindPath(root.left,target);
            FindPath(root.right,target);
        list.remove(list.size()-1);
        return allList;
    }
}

遇到的问题:
之前写的一版是
在这里插入图片描述
没有通过,改成allList.add(new ArrayList<Integer>(list));之后就通过了,因为不重新new的话下一步的操作会改变list的值,也就会改变allList里的值

27.[复杂链表的复制]

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

思路:在这里插入图片描述

图片来自:
链接:https://www.nowcoder.com/questionTerminal/f836b2c43afc4b35ad6adc41ec941dba?f=discussion
来源:牛客网

public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        if(pHead == null){
            return null;
        }
        RandomListNode p = pHead;
        //1.复制
        while(p!=null){
            RandomListNode clone = new RandomListNode(p.label);
            clone.next = p.next;
            p.next = clone;
            p = p.next.next;
        }
        //2.建立random的相互关系
        p = pHead;
        while(p!=null){
            if(p.random!=null){
                p.next.random = p.random.next;
                
            }else{
                p.random = null;
            }
            p = p.next.next;
        }
        //3.拆分
        RandomListNode cHead = pHead.next;
        p = pHead;
     
        while(p!=null){//现在是连在一起的,所以不能这样
            RandomListNode c = p.next;
            p.next = c.next;
            p = c.next;
            if(c.next==null){
                c.next =null;
            }else{
                c.next = c.next.next;
            }
           
        }
        
        return cHead;
    }
}

第一次编译的时候出现了这个错误 ,导致没有过:

	//3.拆分
	RandomListNode cHead = pHead.next;
	p = pHead;
	RandomListNode c = cHead;
	while(p!=null){//现在是连在一起的,这样会导致c找不到
	   p.next = p.next.next;
	   p=p.next.next;
	}
	while(c!=null){
	   c.next = c.next.next;
	   c=p.next.next;
	}
	return cHead;

不能这样分开拆,因为p的链接都断了的话,c就找不到了

28.[二叉搜索树与双链表]

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

思路一:
利用中序遍历的顺序来增加新的指针指向,我的第一思路是采用非递归的版本,这样会比较好操作,但是存在一个指针指向的问题,我一开始分了很多种情况,但是分得太细了导致太麻烦了,设置不好指向,而且也没有弄清楚头节点在哪里,后来看了一下别人的代码,只要在一开始就找到头节点,后面的节点顺序就很明确,指向也是很明确,不需要再看它到底有没有左孩子或者右孩子
代码:

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/import java.util.Stack;
public class Solution {
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null){
            return null;
        }
        Stack<TreeNode> s = new Stack();
        TreeNode p = pRootOfTree;
        TreeNode pre = null;
        TreeNode root = null;
        boolean isFirst = true;
        while(p!=null||!s.isEmpty()){//先不加p!=null
       
        while(p!=null){
            s.push(p);
            p = p.left;
        }
        p = s.pop();
        if(isFirst ==true){
            root = p;//定好头节点
            pre = root;
            isFirst =false;//设置为false,保证这个分支只走一次
        }else{
            p.left = pre;
            pre.right = p;
            pre = p;
        }
            p = p.right;
    }
        return root;
}
}

思路二
非递归版本的,分别拿到左右子树的头节点再做拼接,注意找到交界点之后再设置新的指向。步骤是:
1.对于当前节点p,root等于当前节点p;
2.如果有左子树,那么寻找左子树链表表头rootleft,作为新的root,并且找到左子树形成的链表的最右端end节点,设置end.right =p;p.left = end;
3.如果有右子树,那么寻找右子树链表表头rootright,rootright.left=p;p.right = rootright;
代码:

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.Stack;
public class Solution {
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null){
            return null;
        }
        
        TreeNode p = pRootOfTree;
        TreeNode rootleft = null;
        TreeNode rootright = null;
        TreeNode root = pRootOfTree;
        if(p.left!=null){
            rootleft = Convert(p.left);
            root = rootleft;//设定链表的头节点
            //找最右节点
            TreeNode end = rootleft;
            while(end.right!=null){
                end = end.right;
            }
            //设定指向
            end.right = p;
            p.left = end;
        }
        //右子树不为空,则找右子树链表的开头
        if(p.right!=null){
            rootright= Convert(p.right);
            p.right = rootright;
            rootright.left = p;
        }
        
        return root;
    
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值