二叉树 算法小炒

本文介绍了二叉搜索树(BST)的基本概念、遍历方法以及插入和删除操作。同时,讲解了完全二叉树的定义,并提供了计算节点数量、实现树的镜像、计算深度以及判断平衡性的算法。通过实例解析LeetCode和剑指Offer的相关题目,深入理解这些数据结构的操作与特性。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

BST binary search tree

二叉搜索树:任意节点的值要⼤于等于左⼦树所有节点的值,且要⼩于等于右边⼦树的所有节点的值。
BST在这里插入图片描述

遍历

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);
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值