题目描述
从左向右遍历一个数组,通过不断将其中的元素插入树中可以逐步地生成一棵二叉搜索树。
给定一个由不同节点组成的二叉搜索树
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]]
做题思路
每一个节点都必须排在它的子孙节点前面
比如上图所示这棵二叉搜索树
path长度为1时:[12]
path长度为2时:[12,5]或[12,19]
path长度为3时:[12,5,2]或[12,5,9]或[12,5,19]或[12,19,15]或[12,19,5]
(值得注意的是,当选择12其中一个子节点作为第二个元素时,其另一个子节点可以作为第三个元素)
以此类推...
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<List<Integer>> res; //存储最后结果
public List<List<Integer>> BSTSequences(TreeNode root) {
res=new ArrayList<>(); //初始化
List<Integer> path=new ArrayList<>(); //存储合法路径,即可以生成二叉搜索树的数组
//根节点为空返回[[]]
if(root==null){
res.add(path);
return res;
}
List<TreeNode> queue=new LinkedList<>();
queue.add(root); //将根节点添加到队列中
bfs(queue,path);
return res;
}
public void bfs(List<TreeNode> queue,List<Integer> path){
//队列为空说明遍历结束,将最后一个path添加进去
if(queue.isEmpty()){
res.add(new ArrayList<>(path));
return;
}
List<TreeNode> copy=new ArrayList<>(queue); //将queue拷贝一份便于后续回溯
for(int i=0;i<queue.size();i++){
TreeNode node=queue.get(i); //依次取出队列中节点
path.add(node.val); //将该节点值添加进path中(由于先取的根节点,所以一定保证了根在左右子树前)
queue.remove(i); //在队列中移除该节点
if(node.left!=null) queue.add(node.left); //将左子节点加入队列
if(node.right!=null) queue.add(node.right); //将右子节点加入队列
bfs(queue,path);
//恢复path和queue,进行回溯
path.remove(path.size()-1);
queue=new ArrayList<>(copy);
}
}
}