一、二叉搜索树中的搜索 700
1、分析
充分利用二分搜索树的特性,进行二分查找即可!比较简单,直接上代码
2、代码
# 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 searchBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return None
if root.val == val:
return root
elif root.val <val:
return self.searchBST(root.right,val)
else:
return self.searchBST(root.left,val)
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} val
* @return {TreeNode}
*/
var searchBST = function(root, val) {
if(root===null){
return null;
}
if(root.val==val){
return root;
}else if(root.val<val){
return searchBST(root.right,val);
}else{
return searchBST(root.left,val);
}
};
二、二叉搜索树中的插入 701
1、分析
利用BST的特性,整体做二分查找去找给定值应该插入的地方,然后插入即可。具体见代码和注释。
2、代码
# 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 insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if not root: #找到插入点了,直接插入即可
return TreeNode(val)
if root.val < val:
root.right = self.insertIntoBST(root.right,val)
elif root.val >val:
root.left = self.insertIntoBST(root.left,val)
return root
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} val
* @return {TreeNode}
*/
var insertIntoBST = function(root, val) {
if(root===null){
return new TreeNode(val); //特别注意!!!js的构造函数要通过new调用啊!!!啊啊啊啊,我老忘记。
}
if(root.val<val){
root.right = insertIntoBST(root.right,val);
}else if(root.val>val){
root.left = insertIntoBST(root.left,val);
}
return root;
};
三、删除二叉树中的节点 450
1、分析
分两大步,首先第一步是找到相应节点。然后就是进行删除操作。删除节点有三种情况:
(1)要删除的节点是个叶子节点,所以直接返回null就相当于删掉该节点了
(2)要删除的节点存在左子树或者右子树,那么直接返回存在的子树作为本身替代即可
(3)左右子树都存在,那么这个删除方法就比较多了,这里采用一个简单的方法,就是将原来的右子树全部接替到原来左子树的最右边,然后用左子树代替当前节点返回,这样就可以达到目的了。(还可以找到右子树中的最左节点,然后在右子树中删除该节点,然后将原来左子树作为该节点的左子树,原来的右子树作为该节点的右子树,最后返回这个节点作为原来根节点的替代)
具体见代码
2、代码
# 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 deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return null
if root.val == key:
if not root.left: #若只有一边的子树,那么直接用子树接替当前结点;若没有子树,直接删除,那么返回None就行
return root.right
elif not root.right:
return root.left
else: #左右子树都存在,所以我们将右子树接在左子树的最右端,然后用原来的左子树接替当前结点即可
cur = root.left
while cur.right:
cur = cur.right
cur.right = root.right
return root.left
elif root.val<key:
root.right = self.deleteNode(root.right,key) #不要忘记了接住返回值
else:
root.left = self.deleteNode(root.left,key)
return root
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.right===null){
return root.left;
}else if(root.left===null){
return root.right;
}else{
let cur = root.left;
while(cur.right!==null){
cur = cur.right;
}
cur.right = root.right;
return root.left;
}
}else if(root.val < key){
root.right = deleteNode(root.right,key);
}else{
root.left = deleteNode(root.left,key);
}
return root;
};
四、验证二叉搜索树 98
1、分析
要满足BST,那么要保证左子树中所有节点的值都小于根节点,右子树中所有节点的值都大于根节点。所以我们采用辅助函数,并将根节点的信息,以参数的形式传递下去。
代码如下
2、代码
# 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 helper(self,node,minNode,maxNode): #用参数将 root的约束传递给整个子树
if not node:
return True
if minNode and node.val<=minNode.val: #此处一定注意等号!!!
return False
if maxNode and node.val>=maxNode.val:
return False
# print('@@minNode:',minNode,'@@maxNode',maxNode)
return self.helper(node.left,minNode,node) and self.helper(node.right,node,maxNode)
def isValidBST(self, root: Optional[TreeNode]) -> bool:
return self.helper(root,None,None)
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
* @return {boolean}
*/
var isValidBST = function(root) {
var helper = (node,minNode,maxNode)=>{
if(node===null){
return true;
}
if(minNode!==null && node.val<=minNode.val){ //注意等号!!!
return false;
}
if(maxNode!==null && node.val>=maxNode.val){
return false;
}
return helper(node.left,minNode,node) && helper(node.right,node,maxNode);
}
return helper(root,null,null);
};