首页 > 编程语言 >#yyds干货盘点# LeetCode程序员面试金典:二叉搜索树序列

#yyds干货盘点# LeetCode程序员面试金典:二叉搜索树序列

时间:2022-12-26 18:06:09浏览次数:39  
标签:yyds cur 金典 List queue add path root LeetCode

题目:

从左向右遍历一个数组,通过不断将其中的元素插入树中可以逐步地生成一棵二叉搜索树。

给定一个由不同节点组成的二叉搜索树 root,输出所有可能生成此树的数组。

 

示例 1:

输入: root = [2,1,3]
输出: [[2,1,3],[2,3,1]]
解释: 数组 [2,1,3]、[2,3,1] 均可以通过从左向右遍历元素插入树中形成以下二叉搜索树
2
/ \
1 3

示例 2:

输入: root = [4,1,null,null,3,2]
输出: [[4,1,3,2]]

代码实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<List<Integer>> ans;

public List<List<Integer>> BSTSequences(TreeNode root) {
ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
// 如果 root==null 返回 [[]]
if (root == null) {
ans.add(path);
return ans;
}
List<TreeNode> queue = new LinkedList<>();
queue.add(root);
// 开始进行回溯
bfs(queue, path);
return ans;
}

/**
* 回溯法+广度优先遍历.
*/
private void bfs(List<TreeNode> queue, List<Integer> path) {
// queue 为空说明遍历完了,可以返回了
if (queue.isEmpty()) {
ans.add(new ArrayList<>(path));
return;
}
// 将 queue 拷贝一份,用于稍后回溯
List<TreeNode> copy = new ArrayList<>(queue);
// 对 queue 进行循环,每循环考虑 “是否 「将当前 cur 节点从 queue 中取出并将其左右子
// 节点加入 queue ,然后将 cur.val 加入到 path 末尾」 ” 的情况进行回溯
for (int i = 0; i < queue.size(); i++) {
TreeNode cur = queue.get(i);
path.add(cur.val);
queue.remove(i);
// 将左右子节点加入队列
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
bfs(queue, path);
// 恢复 path 和 queue ,进行回溯
path.remove(path.size() - 1);
queue = new ArrayList<>(copy);
}
}
}

标签:yyds,cur,金典,List,queue,add,path,root,LeetCode
From: https://blog.51cto.com/u_13321676/5969602

相关文章

  • #yyds干货盘点# LeetCode程序员面试金典:检查子树
    题目:检查子树。你有两棵非常大的二叉树:T1,有几万个节点;T2,有几万个节点。设计一个算法,判断T2是否为T1的子树。如果T1有这么一个节点n,其子树与T2一模一样,则T2为T1......
  • #yyds干货盘点# 名企真题专题:编码
    1.简述:描述假定一种编码的编码范围是a~y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下:a,aa,aaa,aaaa,aaab,aaac,……,b,ba,baa,b......
  • leetcode-17. 电话号码的字母组合
    17.电话号码的字母组合给定一个仅包含数字2-9的字符串,返回所有它能表示的字母组合。答案可以按任意顺序返回。给出数字到字母的映射如下(与电话按键相同)。注意1不对应......
  • Leetcode207
    numCourses->总的课程数目Prerequisited->pairinalistdenotinghavetofinishbtostudya class Solution:    def canFinish(self, numCourses: int......
  • [LeetCode] 1759. Count Number of Homogenous Substrings
    Givenastring s,return thenumberof homogenous substringsof s. Sincetheanswermaybetoolarge,returnit modulo 109 +7.Astringis homogenou......
  • leetcode 451. 根据字符出现频率排序
    一、题目给定一个字符串s,根据字符出现的频率对其进行降序排序。一个字符出现的频率是它出现在字符串中的次数。返回已排序的字符串 。如果有多个答案,返回其中......
  • leetcode-521. 最长特殊序列 Ⅰ
    521.最长特殊序列Ⅰ-力扣(Leetcode)脑筋急转弯funcfindLUSlength(astring,bstring)int{ifa!=b{returnmax(len(a),len(b))}retur......
  • #yyds干货盘点# 名企真题专题:拜访
    1.简述:描述现在有一个城市销售经理,需要从公司出发,去拜访市内的某位商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他每次移动只能在左右中选择一个方向 或在......
  • #yyds干货盘点# LeetCode程序员面试金典:首个共同祖先
    题目:设计并实现一个算法,找出二叉树中某两个节点的第一个共同祖先。不得将其他的节点存储在另外的数据结构中。注意:这不一定是二叉搜索树。例如,给定如下二叉树:root=[3,5,......
  • #yyds干货盘点# 名企真题专题:将满二叉树转换为求和树
    1.简述:描述给出满二叉树的前序遍历结果和中序遍历结果,编写算法将其转化为求和树什么是求和树:二叉树的求和树,是一颗同样结构的二叉树,其树中的每个节点将包含原始树中的左子......