【树】B014_二叉树中的列表(双重递归 | bfs+dfs)

一、题目描述

Given a binary tree root and a linked list with head as the first node. 

Return True if all the elements in the linked list starting from the head 
correspond to some downward path connected in the binary tree otherwise return False.

In this context downward path means a path that starts at some node and goes downwards.

二、题解

这道题是周赛题,这里在写一边。

方法一:双重递归

递归套路:

  • 结束条件:
    • 如果 head 遍历到空,证明在树中可以找到一个路径,return true。
    • 如果 root 为空,证明找不到一条路径。
    • 如果 head.val == root.val,则可以从该结点出发继续搜索。
    • 如果 head.val != root.val,则链表不动,树继续向左右递归。

需要注意的点有二:

  • 在主方法的 if (head.val == root.val) { 中不能直接写以下语句,比如测试用例为时
    [1,10]
    [1,null,1,10,1,9]
    
    递归右子树时,因为 head.next.val != root.right.val 而直接 return false,导致结果为 false,与结果相反。
  • 在写递归结束条件时,应该将链表的判空写在数的判空的前面,因为树为空时,有可能是两边的任意一边空。并不能保证树全空。
return dfs(head.next, root.left) || dfs(head.next, root.right)
public boolean isSubPath(ListNode head, TreeNode root) {
  if (head == null)   return true;
  if (root == null)   return false;
  if (head.val == root.val) {	//1
    if (dfs(head.next, root.left) || dfs(head.next, root.right))
      return true;
  }
  return isSubPath(head, root.left) || isSubPath(head, root.right);
}
private boolean dfs(ListNode head, TreeNode root) {
  if (head == null)		//2
      return true;
  if (root == null)
      return false;
  if (head.val != root.val)
      return false;
  return dfs(head.next, root.left) || dfs(head.next, root.right);
}

方法二:bfs + dfs

用队列保存每一层的结点,当遇到匹配的结点,则从该结点 dfs。

public boolean isSubPath(ListNode head, TreeNode root) {
  Queue<TreeNode> queue  = new LinkedList<>();
  queue.add(root);
  while (!queue.isEmpty()) {
      TreeNode node = queue.poll();
      if (node.val == head.val) {
          if (dfs(head, node))
              return true;
      }
      if (node.left != null)  queue.add(node.left);
      if (node.right != null) queue.add(node.right);
  }
  return false;
}
private boolean dfs(ListNode head, TreeNode root) {
  if (head == null)
      return true;
  if (root == null)
      return false;
  if (head.val != root.val)
      return false;
  return dfs(head.next, root.left) || dfs(head.next, root.right);
}

复杂度分析

  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)
发布了495 篇原创文章 · 获赞 105 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/104866859