学习二叉树
follow:代码随想录
538 把二叉搜索树转为累加树
题目描述:给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
迭代法:
public TreeNode convertBST(TreeNode root) {
//反序中序遍历
Deque<TreeNode> stack = new LinkedList<>();
if(root != null) stack.addFirst(root);
else return root;
TreeNode pre = null;
while(!stack.isEmpty()){
TreeNode node = stack.removeFirst();
if(node != null){
if(node.left != null) stack.addFirst(node.left);
stack.addFirst(node);
stack.addFirst(null);
if(node.right != null) stack.addFirst(node.right);
}else{
node = stack.removeFirst();
if(pre != null) node.val = node.val + pre.val;
pre = node;
}
}
return root;
}
递归法:
//递归法
TreeNode pre = null;
public TreeNode convertBST(TreeNode root) {
inorder(root);
return root;
}
public void inorder(TreeNode root){
if(root == null) return;
inorder(root.right);
if(pre != null) root.val += pre.val;
pre = root;
inorder(root.left);
}
108. 将有序数组转换为二叉搜索树
题目描述:
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
输入:nums = [-10,-3,0,5,9]
public TreeNode sortedArrayToBST(int[] nums) {
return sortSubBST(nums, 0, nums.length);
}
//左闭右开
public TreeNode sortSubBST(int[] nums, int left, int right){
int length = right - left;
if(length == 0) return null;
//注意这个地方
int middle = length / 2 + left;
TreeNode root = new TreeNode(nums[middle]);
root.left = sortSubBST(nums, left, middle);
root.right = sortSubBST(nums, middle + 1, right);
return root;
}
669. 修剪二叉搜索树
题目描述:
给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
解法一:显式地删除
class Solution {
TreeNode pre = null;
public TreeNode trimBST(TreeNode root, int low, int high) {
//先处理根节点,使根节点的值在low和high之间
while(root != null && (root.val < low || root.val > high)){
if(root.val < low) root = root.right;
if(root.val > high) root = root.left;
}
if(root == null) return null;
pre = root;
trim(root, low, high);
return(root);
}
public void trim(TreeNode root, int low, int high){
if(root == null) return;
//剔除小于low的值
if(root.val < low){
pre.left = root.right;
root.left = null;
root = pre;
}
//剔除大于high的值
if(root.val > high){
pre.right = root.left;
root.right = null;
root = pre;
}
pre = root;
trim(root.left, low, high);
pre = root;
trim(root.right, low, high);
}
}
解法二:递归返回节点
public TreeNode trimBST(TreeNode root, int low, int high) {
if(root == null) return null;
//如果当前的值小于low,那么应该左子树的所有值都小于low,则找到右子树符合条件的节点,即递归处理右子树
if(root.val < low) return trimBST(root.right, low, high);
if(root.val > high) return trimBST(root.left, low, high);
//处理下一层
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
701.二叉树的插入
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null) return new TreeNode(val);
TreeNode pre = null;
TreeNode org_root =root;
while(root != null){
pre = root;
if(root.val > val){
root = root.left;
}else{
root = root.right;
}
}
if(pre.val > val) pre.left = new TreeNode(val);
else pre.right = new TreeNode(val);
return org_root;
}
450 二叉搜索树的删除
递归法:
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return null;
if(root.val == key){
if(root.right == null) return root.left;
if(root.left == null) return root.right;
else{
TreeNode cur = root.right;
while(cur.left != null){
cur = cur.left;
}
cur.left = root.left;
return root.right;
}
}
if(root.val > key) root.left = deleteNode(root.left, key);
if(root.val < key) root.right = deleteNode(root.right, key);
return root;
}