代码随想录Day 20 - 二叉树
654.最大二叉树
给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
创建一个根节点,其值为 nums 中的最大值。
递归地在最大值 左边 的 子数组前缀上 构建左子树。
递归地在最大值 右边 的 子数组后缀上 构建右子树。
返回 nums 构建的 最大二叉树 。
构造树一般采用的是前序遍历,因为先构造中间节点,然后递归构造左子树和右子树。
通过下标索引直接在原数组上操作,而不是每次构造一个新的数组。
单层递归的逻辑:先要找到数组中最大的值和对应的下标, 最大的值构造根节点,下标用来下一步分割数组。最大值所在的下标左区间 构造左子树;最大值所在的下标右区间 构造右子树。
递归法(前序遍历)
完整代码如下:
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return construct(nums, 0, nums.length);
}
public TreeNode construct(int[] nums, int leftIndex, int rightIndex){
//没有元素了,终止条件
if(rightIndex - leftIndex < 1){
return null;
}
int index = leftIndex; //记录最大值的下标
int maxvalue = nums[leftIndex]; //记录最大值
for(int i = leftIndex + 1; i < rightIndex; i++){
if(nums[i] > maxvalue){
maxvalue = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(maxvalue); //中
//区间:左闭右开
root.left = construct(nums, leftIndex, index); //左
root.right = construct(nums, index + 1, rightIndex); //右
return root;
}
}
617. 合并二叉树
给你两棵二叉树: root1 和 root2 。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始。
递归法(前序遍历)
单层递归的逻辑:把两棵树的元素加到一起。
root1 的左子树是:合并 root1左子树 root2左子树之后的左子树。
root1 的右子树:是 合并root1右子树 root2右子树之后的右子树。
最终root1就是合并之后的根节点。
完整代码如下:
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
//终止条件
if(root1 == null){
return root2; // 如果root1为空,合并之后就应该是root2
}
if(root2 == null){
return root1; // 如果root2为空,合并之后就应该是root1
}
root1.val = root1.val + root2.val; //中
root1.left = mergeTrees(root1.left, root2.left); //左
root1.right = mergeTrees(root1.right, root2.right); //右
return root1;
}
}
迭代法(层序遍历)
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null){
return root2;
}
if(root2 == null){
return root1;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root1);
deque.offer(root2);
while(!deque.isEmpty()){
TreeNode node1 = deque.poll();
TreeNode node2 = deque.poll();
node1.val = node1.val + node2.val;
// 如果两棵树左节点都不为空,加入队列
if(node1.left != null && node2.left != null){
deque.offer(node1.left);
deque.offer(node2.left);
}
// 如果两棵树右节点都不为空,加入队列
if(node1.right != null && node2.right != null){
deque.offer(node1.right);
deque.offer(node2.right);
}
// 若node1的左节点为空,直接赋值
if(node1.left == null && node2.left != null){
node1.left = node2.left;
}
// 若node1的右节点为空,直接赋值
if(node1.right == null && node2.right != null){
node1.right = node2.right;
}
}
return root1;
}
}
700. 二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
递归法
二叉搜索树的单层递归逻辑:
因为二叉搜索树的节点是有序的,所以可以有方向的去搜索。
如果root.val > val,搜索左子树,如果root.val < val,就搜索右子树,最后如果都没有搜索到,就返回NULL。
class Solution {
// 递归,利用二叉搜索树特点
public TreeNode searchBST(TreeNode root, int val) {
if(root == null || root.val == val) return root;
if(val < root.val){
return searchBST(root.left, val);
}else{
return searchBST(root.right, val);
}
}
}
迭代法
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while(root != null){
if(root.val == val){
return root;
}else if(root.val > val){
root = root.left;
}else if(root.val < val){
root = root.right;
}
}
return null;
}
}
98.验证二叉搜索树
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
中序遍历下,输出的二叉搜索树节点的数值是有序序列。有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的。按照这个思路,可以先递归中序遍历将二叉搜索树转变成一个数组,然后再判断是否递增。
递归法(中序遍历)
递归函数要有bool类型的返回值,因为我们在寻找一个不符合条件的节点,如果没有找到这个节点就遍历了整个树,如果找到不符合的节点了,立刻返回。
class Solution {
TreeNode max;
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
boolean left = isValidBST(root.left); //左
//注意=号
if(max != null && max.val >= root.val){
return false;
}
max = root; //中
boolean right = isValidBST(root.right); //右
return left && right;
}
}
迭代法(中序遍历)
class Solution {
TreeNode max;
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
if(max != null && cur.val <= max.val){
return false;
}
max = cur;
cur = cur.right;
}
}
return true;
}
}