数据结构—广度有限搜索

1 链表的中间结点
题目:给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
示例 2:
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode current=head;
        int lengeth=0;
        while(current!=null){
            lengeth++;
            current=current.next;
        }
        lengeth=lengeth/2+1;
        for(int i=1;i<lengeth;i++){
            head=head.next;
        }
        return head;
    }
}

方法二

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        List<ListNode> list = new LinkedList<ListNode>();
        ListNode current = head;
        int count = 0;
        while(current!=null) {
            list.add(current);
            current = current.next;
            count++;
        }
        count = count/2+1;
        return list.get(count-1);
    }
}

2 从上到下打印二叉树 II
题目:从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如:
给定二叉树: [3,9,20,null,null,15,7],
思路:I. 按层打印: 题目要求的二叉树的 从上至下 打印(即按层打印),又称为二叉树的 广度优先搜索(BFS)。BFS 通常借助 队列 的先入先出特性来实现。
II. 每层打印到一行: 若将二叉树从上至下分为多层,则通过访问 某层所有节点的 左右子节点 ,可统计出 下一层的所有节点 。根据此特性,可在打印本层全部节点时,将下一层全部节点加入队列,以此类推,即可分为多行打印。
在这里插入图片描述

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            res.add(tmp);
        }
        return res;
    }
}

3 二叉树的层次遍历 II
题目: 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
解法一:BFS
常规的层序遍历稍加修改。一次把同一层的顶点全取出来,并把属于下一层的所有顶点一次性入队。然后每次都将subList插入到res的头。
具体做法:把根结点入队,此时队列的大小为1,表示当前层含有的结点数也为1,这是显然的,然后将子结点入列后进入下一轮循环。此后,每一层的结点数都可以用队列的大小来表示,这样就做到了一次把整层的结点都处理了,将它们全部出列,并将它们的子结点入列,使得此时队列里含有的顶点都是下一层的顶点。

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> res = new LinkedList<>();
        if(root == null)    return res;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            LinkedList<Integer> subList = new LinkedList<>();
            int sz = q.size();
            for(int i = 0; i < sz; i++)
            {
                TreeNode cur = q.poll();
                subList.offer(cur.val);
                if(cur.left != null)    q.offer(cur.left);
                if(cur.right != null)   q.offer(cur.right);
            }
            res.offerFirst(subList);
        }
        return res;
    }
}

解法二:DFS
DFS递归遍历,但是多使用一个形参,记录当前层号。如果子列表的数量小于层数,说明第一次到达该层,为结果添加一个子列表。注意为了实现自底向上输出,每次都将子列表添加到结果开头。子列表的下标对应的是自底向上遍历时结点所在的层号,即0对应最后一层。然后对遍历到的顶点,将其添加到所在层号对应的子列表中即可。

class Solution {
    private LinkedList<List<Integer>> ans;
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        ans = new LinkedList<>();
        getOrder(root, 1);
        return ans;
    }
    private void getOrder(TreeNode root, int level) {
        if(root == null)    return;
        if(ans.size() < level)
            ans.addFirst(new LinkedList<Integer>());
        List<Integer> subList = ans.get(ans.size() - level);
        subList.add(root.val);
        getOrder(root.left, level + 1);
        getOrder(root.right, level + 1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值