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 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 1、二叉树算法相关 -> 正文阅读

[数据结构与算法]1、二叉树算法相关

二叉树

二叉树最重要的是先知道当前root的节点需要做什么,然后再根据函数定义进行递归调用子节点,如何提炼出叶子结点要做的事情也正是二叉树的难点所在。

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
}

排序

前序遍历:中–>左–>右
中序遍历:左–>中–>右
后续遍历:左–>右–>中

94. 二叉树的中序遍历

https://leetcode.cn/problems/binary-tree-inorder-traversal/

// 递归法
class Solution {
	
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorder(root, result);
        return result;
    }
   // 前序遍历
    public  void preorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        inorder(root.left, result);
        inorder(root.right, result);
    }
    // 中序遍历
    public  void inorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        inorder(root.left, result);
        result.add(root.val);
        inorder(root.right, result);
    }
    // 后序遍历
    public  void postorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        inorder(root.left, result);
        inorder(root.right, result);
        result.add(root.val);
    }
     
}

101.判断二叉树是否对称

https://leetcode.cn/problems/symmetric-tree/
理思路
1、如果二叉树根节点为空,那么为对称二叉树
2、如果左子树 == 右子树,那么为对称二叉树
3、如何判断左子树 == 右子树? 左子树的左子节点右子树的右子节点,左子树的右子节点右子树的左子节点

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root==null){
            return true;
        }
        return cmp(root.left, root.right);
    }

    public boolean cmp(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return true;
        }
        if (node1 == null || node2 == null) {
            return false;
        }
        return node1.val==node2.val && cmp(node1.left, node2.right) && cmp(node1.right, node2.left);
    }
}

104. 二叉树的最大深度

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

当前节点要做的事情:返回当前节点的最大深度,然后+1(当前节点)

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));
    }
}

226. 翻转二叉树

https://leetcode.cn/problems/invert-binary-tree/
当前节点要做的事情:左右子树交换

Class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;

        invertTree(root.left);
        invertTree(root.right);
        
        return root;
    }
}

543. 二叉树的直径

https://leetcode.cn/problems/diameter-of-binary-tree/
当前节点要做的事情:左右子树的高度和的最大值

class Solution {
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        setDepth(root);
        return max;
    }

    int max = 0;

    public int setDepth(TreeNode treeNode) {
        if (treeNode == null) {
            return 0;
        }
        int left = setDepth(treeNode.left);
        int right = setDepth(treeNode.right);
        if (right + left > max) {
            max = left + right;
        }
        return Math.max(left, right) + 1;
    }
}

617. 合并二叉树

https://leetcode.cn/problems/merge-two-binary-trees/
当前节点要做的事情:判断节点是否为null,全为空,部分为空,都不为空

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        root1.val = root1.val + root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-06-29 19:19:22  更:2022-06-29 19:21:41 
 
开发: 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年4日历 -2024/4/27 1:07:19-

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