提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
BST binary search tree
二叉搜索树:任意节点的值要⼤于等于左⼦树所有节点的值,且要⼩于等于右边⼦树的所有节点的值。
遍历
1.通用遍历
代码如下(示例):
boolean isInBST(TreeNode root, int target) {
if (root == null) return false;
if (root.val == target) return true;
return isInBST(root.left, target)
|| isInBST(root.right, target);
}
2.BST遍历
代码如下(示例):
void BST(TreeNode root, int target) {
if (root.val == target)
// 找到⽬标, 做点什么
if (root.val < target)
BST(root.right, target);
if (root.val > target)
BST(root.left, target);
}
插入
⼀旦涉及“改”, 函数就要返回 TreeNode 类型;
TreeNode insertIntoBST(TreeNode root, int val) {
// 找到空位置插⼊新节点
if (root == null) return new TreeNode(val);
// if (root.val == val)
// BST 中⼀般不会插⼊已存在元素
if (root.val < val)
root.right = insertIntoBST(root.right, val);
if (root.val > val)
root.left = insertIntoBST(root.left, val);
return root;
}
删除
⼀旦涉及“改”, 函数就要返回 TreeNode 类型;
TreeNode deleteNode(TreeNode root, int key) {
if (root.val == key) {
// 找到啦, 进⾏删除,右⼦的最小值接替⾃⼰的位置
if (root.left == null && root.right == null)
return null;
if (root.left == null) return root.right;
if (root.right == null) return root.left;
if (root.left != null && root.right != null) {
// 找到右⼦树的最⼩节点
TreeNode minNode = getMin(root.right);
// 把 root 改成 minNode
root.val = minNode.val;
// 转⽽去删除 minNode
root.right = deleteNode(root.right, minNode.val);
}
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
} return root;
TreeNode getMin(TreeNode node) {
// BST 最左边的就是最⼩的
while (node.left != null) node = node.left;
return node;
}
}
完全二叉树
定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
练习
leetcode222. 完全二叉树的节点个数
根,左, 右
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==NULL) return 0;
int left = countNodes(root->left);
int right = countNodes(root->right);
return left+right+1;
}
};
Offer 27. 二叉树的镜像
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root==NULL) return root;
TreeNode* temp = new TreeNode(0);
temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->left);
mirrorTree(root->right);
return root;
}
};
剑指 Offer 55 - I. 二叉树的深度
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL) return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return left>right?left+1:right+1;
}
};
剑指 Offer 55 - II. 判断是否为平衡二叉树
class Solution {
public:
bool isBalanced(TreeNode* root) {
if (root==NULL) return true;
if(abs(hight(root->left)-hight(root->right))>1) return false;
if(!isBalanced(root->left)) return false;
if(!isBalanced(root->right)) return false;
return true;
}
int hight(TreeNode* root){
if(root==NULL) return 0;
return max(hight(root->left),hight(root->right))+1;
}
};
官方
class Solution {
public:
int height(TreeNode* root) {
if (root == NULL) {
return 0;
} else {
return max(height(root->left), height(root->right)) + 1;
}
}
bool isBalanced(TreeNode* root) {
if (root == NULL) {
return true;
} else {
return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
}
};