IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 搞搞算法 2: 二叉树 -> 正文阅读

[数据结构与算法]搞搞算法 2: 二叉树

参考:https://github.com/labuladong/fucking-algorithm

二叉树

力扣104 二叉树的最大深度

https://leetcode.cn/problems/maximum-depth-of-binary-tree/

方法一:遍历二叉树

class Solution {
? ? // 当前深度
? ? int depth = 0;
? ? // 最大深度
? ? int res = 0;

? ? /**
   ? * 主方法
?    */
? ? public int maxDepth(TreeNode root) {
? ? ? ? traverse(root);
? ? ? ? return res;
? ? }

? ? /**
? ? ?* 遍历二叉树
? ? ?*/
? ? public void traverse(TreeNode root){
? ? ? ? if(root == null){
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? // 前序位置
? ? ? ? depth++;
? ? ? ? if(root.left == null && root.right == null){
? ? ? ? ? ? res = Math.max(depth, res);
? ? ? ? }
? ? ? ? // 处理左子树
? ? ? ? traverse(root.left);
? ? ? ? // 中序位置(无内容)
? ? ? ? // 处理右子树
? ? ? ? traverse(root.right);
? ? ? ? // 后序位置
? ? ? ? depth--;
? ? }
}

方法二:问题分解

class Solution {
? ? public int maxDepth(TreeNode root) {
? ? ? ? if(root == null){
? ? ? ? ? ? return 0;
? ? ? ? }
? ? ? ? // 处理左子树
? ? ? ? int leftDepth = maxDepth(root.left);
? ? ? ? // 处理右子树
? ? ? ? int rightDepth = maxDepth(root.right);
? ? ? ? return Math.max(leftDepth, rightDepth) + 1;
? ? }
}

力扣543 二叉树的直径

https://leetcode.cn/problems/diameter-of-binary-tree/

class Solution {
? ? // 二叉树的直径
? ? int res = 0;

? ? // 主方法
? ? public int diameterOfBinaryTree(TreeNode root) {
? ? ? ? depthOfBinaryTree(root);
? ? ? ? return res;
? ? }

? ? /**
? ? ?* 二叉树的深度
? ? ?*/
? ? public int depthOfBinaryTree(TreeNode root){
? ? ? ? if(root == null){
? ? ? ? ? ? return 0;
? ? ? ? }
? ? ? ? int leftDepth = depthOfBinaryTree(root.left);
? ? ? ? int rightDepth = depthOfBinaryTree(root.right);
? ? ? ? // 顺便计算二叉树的直径
? ? ? ? res = Math.max(res, leftDepth + rightDepth);
? ? ? ? return Math.max(leftDepth, rightDepth) + 1;
? ? }
}

力扣226 翻转二叉树

https://leetcode.cn/problems/invert-binary-tree/

方法一:遍历二叉树

class Solution {
? ? /**
? ? ?* 主方法
? ? ?*/
? ? public TreeNode invertTree(TreeNode root) {
? ? ? ? traverse(root);
? ? ? ? return root;
? ? }

? ? /**
? ? ?* 遍历二叉树
? ? ?*/
? ? public void traverse(TreeNode root){
? ? ? ? if(root == null){
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? TreeNode tn = root.left;
? ? ? ? root.left = root.right;
? ? ? ? root.right = tn;
? ? ? ? traverse(root.left);
? ? ? ? traverse(root.right);
? ? }
}

方法二:问题分解

class Solution {
? ? public TreeNode invertTree(TreeNode root) {
? ? ? ? if(root == null){
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? TreeNode tn = invertTree(root.left);
? ? ? ? root.left = invertTree(root.right);
? ? ? ? root.right = tn;
? ? ? ? return root;
? ? }
}

力扣116 填充每个结点的下一个右侧结点指针

https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/

class Solution {
????/**
?????*?主方法
?????*/
? ? public Node connect(Node root) {
? ? ? ? if(root == null){
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? traverse(root.left, root.right);
? ? ? ? return root;
? ? }

    /**
     * 遍历二叉树
     */
? ? public void traverse(Node left, Node right){
? ? ? ? if(left == null || right == null){
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? left.next = right;
? ? ? ? traverse(left.left, left.right);
? ? ? ? traverse(right.left, right.right);
? ? ? ? // 连接左右子树
? ? ? ? traverse(left.right, right.left);
? ? }
}

力扣114 二叉树展开为链表

https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/

class Solution {
    public void flatten(TreeNode root) {
        if(root == null){
            return;
        }
        // 处理左子树
        flatten(root.left);
        // 处理右子树
        flatten(root.right);
        // 处理当前结点
        TreeNode left = root.left;
        TreeNode right = root.right;
        root.left = null;
        root.right = left;
        TreeNode tn = root;
        while(tn.right != null){
            tn = tn.right;
        }
        tn.right = right;
    }
}

力扣654 最大二叉树

https://leetcode.cn/problems/maximum-binary-tree/

class?Solution?{
????/**
?????*?主方法
?????*/
????public?TreeNode?constructMaximumBinaryTree(int[]?nums){
????????return?constructMaximumBinaryTree(nums,?0,?nums.length?-?1);
????}

????/**
?????*?构造最大二叉树
?????*/
????public?TreeNode?constructMaximumBinaryTree(int[]?nums,?int?start,?int?end)?{
????????if(start?>?end){
????????????return?null;
????????}
????????int?maxValueIndex?=?getMaxValueIndex(nums,?start,?end);
????????TreeNode?root?=?new?TreeNode(nums[maxValueIndex]);
????????root.left?=?constructMaximumBinaryTree(nums,?start,?maxValueIndex?-?1);
????????root.right?=?constructMaximumBinaryTree(nums,?maxValueIndex?+?1,?end);
????????return?root;
????}

????/**
?????*?获取最大值对应的数组下标
?????*/
????public?int?getMaxValueIndex(int[]?nums,?int?start,?int?end){
????????int?index?=?-1;
????????int?maxValue?=?Integer.MIN_VALUE;
????????for(int?i?=?start;?i?<=?end;?i++){
????????????if(nums[i]?>?maxValue){
????????????????index?=?i;
????????????????maxValue?=?nums[i];
????????????}
????????}
????????return?index;
????}
}

力扣105 从前序与中序遍历序列构造二叉树

https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

class?Solution?{
????Map<Integer,?Integer>?valToIndexMap?=?new?HashMap();

????/**
?????*?主方法
?????*/
????public?TreeNode?buildTree(int[]?preorder,?int[]?inorder)?{
????????for?(int?i?=?0;?i?<?inorder.length;?i++)?{
????????????valToIndexMap.put(inorder[i],?i);
????????}
????????return?build(preorder,?inorder,?0,?preorder.length?-?1,?0,?inorder.length?-?1);
????}

????/**
?????*?构造二叉树
?????*/
????public?TreeNode?build(int[]?preorder,?int[]?inorder,?int?preLeft,?int?preRight,?int?inLeft,?int?inRight){
????????if(preLeft?>?preRight){
????????????return?null;
????????}
????????TreeNode?root?=?new?TreeNode(preorder[preLeft]);
????????int?index?=?valToIndexMap.get(preorder[preLeft]);

????????root.left?=?build(preorder,?inorder,?preLeft?+?1,?preLeft?+?(index?-?inLeft),?inLeft,?index?-?1);
????????root.right?=?build(preorder,?inorder,?preLeft?+?(index?-?inLeft)?+?1,?preRight,?index?+?1,?inRight);
????????return?root;
????}
}

力扣106 从中序与后序遍历序列构造二叉树

https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

class?Solution?{
????Map<Integer,?Integer>?valToIndexMap?=?new?HashMap();

????/**
?????*?主方法
?????*/
????public?TreeNode?buildTree(int[]?inorder,?int[]?postorder)?{
????????for?(int?i?=?0;?i?<?inorder.length;?i++)?{
????????????valToIndexMap.put(inorder[i],?i);
????????}
????????return?build(inorder,?postorder,?0,?inorder.length?-?1,?0,?postorder.length?-?1);
????}
????/**
?????*?构造二叉树
?????*/
????public?TreeNode?build(int[]?inorder,?int[]?postorder,?int?inLeft,?int?inRight,?int?postLeft,?int?postRight){
????????if(inLeft?>?inRight){
????????????return?null;
????????}
????????TreeNode?root?=?new?TreeNode(postorder[postRight]);
????????int?index?=?valToIndexMap.get(postorder[postRight]);
????????root.left?=?build(inorder,?postorder,?inLeft,?index?-?1,?postLeft,?postLeft?+?(index?-?inLeft)?-?1);
????????root.right?=?build(inorder,?postorder,?index?+?1,?inRight,?postLeft?+?(index?-?inLeft),?postRight?-?1);
????????return?root;
????}
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-09-25 23:20:35  更:2022-09-25 23:21:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 19:44:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码