小黑做法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
# 排序生成中序遍历
midorder = sorted(preorder)
def bulid_tree(pre_list,mid_list):
# 返回空值
if not pre_list:
return None
root_num = pre_list[0]
root = TreeNode(val=root_num)
# 左右子树长度
left_len = mid_list.index(root_num)
right_len = len(mid_list) - 1 - left_len
# 链接左右子树
left = bulid_tree(pre_list[1:left_len+1],mid_list[:left_len])
right = bulid_tree(pre_list[left_len+1:],mid_list[left_len+1:])
root.left = left
root.right = right
return root
return bulid_tree(preorder,midorder)
使用先序遍历和中序遍历构造二叉树(java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode bstFromPreorder(int[] preorder) {
int length = preorder.length;
Map<Integer,Integer> map = new HashMap<>();
// 定义中序数组
int[] midorder = new int[length];
System.arraycopy(preorder,0,midorder,0,length);
Arrays.sort(midorder);
// 构造map
for(int i = 0;i < length;i++){
map.put(midorder[i],i);
}
return bfs(0,length-1,0,length-1,preorder,map);
}
public TreeNode bfs(int p_s,int p_e,int m_s,int m_e,int[] preorder,Map<Integer,Integer> map){
if(p_s > p_e){
return null;
}
// root的数字
int root_num = preorder[p_s];
// root在中序的index
int root_index = map.get(root_num);
TreeNode root = new TreeNode(root_num);
root.left = bfs(p_s+1,root_index-m_s+p_s,m_s,root_index-1,preorder,map);
root.right = bfs(root_index-m_s+p_s+1,p_e,root_index+1,m_e,preorder,map);
return root;
}
}
小黑尝试分界线递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
# 中序遍历结果
def dfs(pre_list):
if not pre_list:
return None
root_num = pre_list[0]
root = TreeNode(val=root_num)
index = len(pre_list) - 1
for t in range(len(pre_list)):
if pre_list[t] > root_num:
index = t - 1
break
root.left = dfs(pre_list[1:index+1])
root.right = dfs(pre_list[index+1:])
return root
return dfs(preorder)
二分查找左右子树(python)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
# 中序遍历结果
def dfs(pre_list):
if not pre_list:
return None
root_num = pre_list[0]
root = TreeNode(val=root_num)
# 二分法双指针
l = 0
r = len(pre_list) - 1
while l < r:
mid = (l + r + 1) // 2
if pre_list[mid] < root_num:
l = mid
else:
r = mid - 1
root.left = dfs(pre_list[1:l+1])
root.right = dfs(pre_list[l+1:])
return root
return dfs(preorder)
二分查找左右子树(java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode bstFromPreorder(int[] preorder) {
return dfs(preorder,0,preorder.length-1);
}
public TreeNode dfs(int[] pre_list,int left,int right){
if(left > right){
return null;
}
int root_num = pre_list[left];
TreeNode root = new TreeNode(root_num);
// 准备二分查找
int mid = -1,l = left,r = right;
while(l < r){
mid = (l + r + 1) / 2;
if(pre_list[mid] < root_num){
l = mid;
}else{
r = mid - 1;
}
}
root.left = dfs(pre_list,left+1,l);
root.right = dfs(pre_list,l+1,right);
return root;
}
}
根据数值上下界递归构建左右子树(python)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
# 定义全局变量
self.index = 0
self.n = len(preorder)
# 定义初始max与min
max_ = inf
min_ = -inf
def dfs(min_value,max_value):
if self.index == self.n:
return None
# 当前结点数字
cur = preorder[self.index]
# 判断当前数字是否满足该范围
if not (min_value <= cur <= max_value):
return None
# 构建结点
self.index += 1
root = TreeNode(val=cur)
root.left = dfs(min_value,cur)
root.right = dfs(cur,max_value)
return root
return dfs(min_,max_)
根据数值上下界递归构建左右子树(java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int index = 0;
int n = -1;
int[] preorder;
public TreeNode bstFromPreorder(int[] preorder) {
this.n = preorder.length;
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
this.preorder = preorder;
return dfs(min,max);
}
public TreeNode dfs(int min,int max){
if(this.index == this.n){
return null;
}
int val = this.preorder[this.index];
if(val < min || val > max){
return null;
}
this.index++;
TreeNode root = new TreeNode(val);
root.left = dfs(min,val);
root.right = dfs(val,max);
return root;
}
}
单调栈(python)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
# 判空操作
if not preorder:
return None
# 定义根节点
root = TreeNode(val = preorder[0])
# 初始化栈
q = [root]
# 遍历每一个结点
for num in preorder[1:]:
cur_node = TreeNode(val = num)
# 寻找cur_node的父节点
pre_node = q[-1]
while q and q[-1].val < cur_node.val:
pre_node = q.pop()
# 判断cur_node是左孩子还是右孩子
if pre_node.val < cur_node.val:
pre_node.right = cur_node
else:
pre_node.left = cur_node
q.append(cur_node)
return root
单调栈(java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int index = 0;
int n = -1;
int[] preorder;
public TreeNode bstFromPreorder(int[] preorder) {
if(preorder.length == 0){
return null;
}
TreeNode root = new TreeNode(preorder[0]);
Stack<TreeNode> q = new Stack<>();
q.push(root);
for(int i = 1;i < preorder.length;i++){
TreeNode cur = new TreeNode(preorder[i]);
TreeNode pre = q.peek();
while(!q.isEmpty() && q.peek().val < cur.val){
pre = q.pop();
}
if(pre.val < cur.val){
pre.right = cur;
}else{
pre.left = cur;
}
q.push(cur);
}
return root;
}
}
小黑生活
宿舍一起学校火锅
justRun一起南二环
周六的北新桥烧烤
周六晚上白嫖的喜茶(糖放多了,退款了还给我重新做了)
周日和大黑驴老黑的火锅与剪发
校园日常大盘鸡&&螺蛳粉&&咖啡&&中药&&啤酒消遣
justRUN朝阳公园撒野