今天又按照视频多刷了几道题,一点一点进步吧,实在是着急不来的。
235. 二叉搜索树的最近公共祖先
相对于 二叉树的最近公共祖先 本题就简单一些了,因为 可以利用二叉搜索树的特性。
题目链接/文章讲解:代码随想录
视频讲解:二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先_哔哩哔哩_bilibili
再次强调一下二叉搜索树的特性,二叉搜索树是一个有序树:
- 若它的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
- 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
- 它的左、右子树也分别为二叉搜索树。
递归法,来看一下JS代码:
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
if (root == null) {
return null;
};
// 左
if (root.val > p.val && root.val > q.val) {
const left = lowestCommonAncestor(root.left, p, q)
if (left != null) {
return left;
}
};
// 右
if (root.val < p.val && root.val < q.val) {
const right = lowestCommonAncestor(root.right, p, q)
if (right != null) {
return right;
}
};
// 找到了想要的结果
return root;
};
迭代法:
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
// 用迭代法来实现
var lowestCommonAncestor = function(root, p, q) {
// if (root == null) {
// return null;
// };
let curr = root;
while (curr) {
if (curr.val > p.val && curr.val > q.val) {
curr = curr.left
} else if (curr.val < p.val && curr.val < q.val) {
curr = curr.right
} else {
return curr;
}
}
return null;
};
701.二叉搜索树中的插入操作
本题比想象中的简单,大家可以先自己想一想应该怎么做,然后看视频讲解,就发现 本题为什么比较简单了。
题目链接/文章讲解:代码随想录
视频讲解:原来这么简单? | LeetCode:701.二叉搜索树中的插入操作_哔哩哔哩_bilibili
解题思路:新插入的节点在叶子节点就可以了,尽量不要改变二叉树的结构,要不然会很复杂。
递归法解决代码如下:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
var insertIntoBST = function(root, val) {
if (root == null) {
let newNode = new TreeNode(val);
return newNode;
};
if (val < root.val) {
root.left = insertIntoBST(root.left, val)
};
if (val > root.val) {
root.right = insertIntoBST(root.right, val)
};
return root;
};
450.删除二叉搜索树中的节点
相对于 插入操作,本题就有难度了,涉及到改树的结构;
题目链接/文章讲解:代码随想录
视频讲解:调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点_哔哩哔哩_bilibili
涉及到5种情况:
- 1、没找到删的点;
- 2、删的点是叶子节点,左空右空;
- 3、删的点,左不空,右空;
- 4、删的点,左为空,右不空;
- 5、删的点,左不空,右不空;要么是左孩子继位,要么是右孩子继位
看一下JS代码:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} key
* @return {TreeNode}
*/
var deleteNode = function(root, key) {
if (root == null) {
return null;
}
if (root.val == key) {
if (!root.left && !root.right) {
return null;
} else if (root.left && !root.right) {
return root.left;
} else if (!root.left && root.right) {
return root.right;
} else {
let curr = root.right;
while(curr.left != null) {
curr = curr.left;
}
curr.left = root.left;
return root.right;
}
}
// 单层递归的逻辑
if (key < root.val) {
root.left = deleteNode(root.left, key)
}
if (key > root.val) {
root.right = deleteNode(root.right, key)
}
return root;
};