码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode常见题型——树


    1. 算法思想

    作为(单)链表的升级版,我们通常接触的树都是二叉树(binary tree),即每个节点最多有
    两个子节点;且除非题目说明,默认树中不存在循环结构。

    1. struct TreeNode {
    2. int val;
    3. TreeNode *left;
    4. TreeNode *right;
    5. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    6. };

    2.常见题型

    2.1 树的递归

    对于一些简单的递归题,某些LeetCode 达人喜欢写one-line code,即用一行代码解决问题,
    把if-else 判断语句压缩成问号冒号的形式。

    LeetCode-104. Maximum Depth of Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, return its maximum depth.https://blog.csdn.net/qq_15711195/article/details/122390238LeetCode-110. Balanced Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given a binary tree, determine if it is height-balanced.https://blog.csdn.net/qq_15711195/article/details/122288238LeetCode-543. Diameter of Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnthe length of thediameterof the tree. Thediameterof a binary tree is thelengthof the longest path between any two nodes in a tree. This path may or may not pass through theroot.https://blog.csdn.net/qq_15711195/article/details/122392821LeetCode-112. Path Sum [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree and an integertargetSum, returntrueif the tree has aroot-to-leafpath such that adding up all the values along the path equalstargetSum. Aleafis a node with no children.https://blog.csdn.net/qq_15711195/article/details/126316873LeetCode-113. Path Sum II [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree and an integertargetSum, returnallroot-to-leafpaths where the sum of the node values in the path equalstargetSum. Each path should be returned as a list of the nodevalues, not node references.https://blog.csdn.net/qq_15711195/article/details/126317281LeetCode-437. Path Sum III [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree and an integertargetSum, returnthe number of paths where the sum of the valuesalong the path equalstargetSum. The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only ...https://blog.csdn.net/qq_15711195/article/details/126239920LeetCode-101. Symmetric Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree,check whether it is a mirror of itself(i.e., symmetric around its center).https://blog.csdn.net/qq_15711195/article/details/126317542LeetCode-572. Subtree of Another Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given the roots of two binary treesrootandsubRoot, returntrueif there is a subtree ofrootwith the same structure and node values ofsubRootandfalseotherwise. A subtree of a binary treetreeis a tree that consists of a node intreeand all of th...https://blog.csdn.net/qq_15711195/article/details/126331459LeetCode-1110. Delete Nodes And Return Forest [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, each node in the tree has a distinct value.After deleting all nodes with a value into_delete, we are left with a forest (a disjoint union of trees). Return the roots of the trees in the remaining forest. https://blog.csdn.net/qq_15711195/article/details/126320648

    2.2 层次遍历

    LeetCode-637. Average of Levels in Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnthe average value of the nodes on each level in the form of an array. Answers within10-5of the actual answer will be accepted.https://blog.csdn.net/qq_15711195/article/details/126321340

    2.3 前中后序遍历

    前序遍历先遍历父结点,再遍历左结点,最后遍历右节点

    1. void preorder(TreeNode* root) {
    2. visit(root);
    3. preorder(root->left);
    4. preorder(root->right);
    5. }

    中序遍历先遍历左节点,再遍历父结点,最后遍历右节点

    1. void inorder(TreeNode* root) {
    2. inorder(root->left);
    3. visit(root);
    4. inorder(root->right);
    5. }

    后序遍历先遍历左节点,再遍历右结点,最后遍历父节点

    1. void postorder(TreeNode* root) {
    2. postorder(root->left);
    3. postorder(root->right);
    4. visit(root);
    5. }

     已知二叉树,求三种序列

    LeetCode-94. Binary Tree Inorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客

    leetcode单词接龙2-Algorithms:算法学习笔记
    zip 星 59KB
    下载

    LeetCode-145. Binary Tree Postorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客

    已知两种序列,重建二叉树

    LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客Given two integer arrayspreorderandinorderwherepreorderis the preorder traversal of a binary tree andinorderis the inorder traversal of the same tree, construct and returnthe binary tree.https://blog.csdn.net/qq_15711195/article/details/122906206?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22122906206%22%2C%22source%22%3A%22qq_15711195%22%7DLeetCode-106. Construct Binary Tree from Inorder and Postorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客Given two integer arraysinorderandpostorderwhereinorderis the inorder traversal of a binary tree andpostorderis the postorder traversal of the same tree, construct and returnthe binary tree.https://blog.csdn.net/qq_15711195/article/details/126339392

    LeetCode-889. Construct Binary Tree from Preorder and Postorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客Given two integer arrays,preorderandpostorderwherepreorderis the preorder traversal of a binary tree ofdistinctvalues andpostorderis the postorder traversal of the same tree, reconstruct and returnthe binary tree.https://blog.csdn.net/qq_15711195/article/details/126334214

    2.4 二叉查找树

    LeetCode-637. Average of Levels in Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnthe average value of the nodes on each level in the form of an array. Answers within10-5of the actual answer will be accepted.https://blog.csdn.net/qq_15711195/article/details/126321340LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客Given two integer arrayspreorderandinorderwherepreorderis the preorder traversal of a binary tree andinorderis the inorder traversal of the same tree, construct and returnthe binary tree.https://blog.csdn.net/qq_15711195/article/details/122906206LeetCode-144. Binary Tree Preorder Traversal [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnthe preorder traversal of its nodes' values.https://blog.csdn.net/qq_15711195/article/details/126322090LeetCode-99. Recover Binary Search Tree [C++][Java]_贫道绝缘子的博客-CSDN博客You are given therootof a binary search tree (BST), where the values ofexactlytwo nodes of the tree were swapped by mistake.Recover the tree without changing its structure.https://blog.csdn.net/qq_15711195/article/details/126328221LeetCode-669. Trim a Binary Search Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary search tree and the lowest and highest boundaries aslowandhigh, trim the tree so that all its elements lies in[low, high]. Trimming the tree shouldnotchange the relative structure of the elementshttps://blog.csdn.net/qq_15711195/article/details/126328363LeetCode-208. Implement Trie (Prefix Tree) [C++][Java]_贫道绝缘子的博客-CSDN博客​Atrie(pronounced as "try") orprefix treeis a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.https://blog.csdn.net/qq_15711195/article/details/126328615LeetCode-226. Invert Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, invert the tree, and returnits root.https://blog.csdn.net/qq_15711195/article/details/126329758LeetCode-572. Subtree of Another Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given the roots of two binary treesrootandsubRoot, returntrueif there is a subtree ofrootwith the same structure and node values ofsubRootandfalseotherwise. A subtree of a binary treetreeis a tree that consists of a node intreeand all of th...https://blog.csdn.net/qq_15711195/article/details/126331459LeetCode-101. Symmetric Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree,check whether it is a mirror of itself(i.e., symmetric around its center).https://blog.csdn.net/qq_15711195/article/details/126317542LeetCode-404. Sum of Left Leaves [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnthe sum of all left leaves. Aleafis a node with no children. Aleft leafis a leaf that is the left child of another node.https://blog.csdn.net/qq_15711195/article/details/126331670LeetCode-513. Find Bottom Left Tree Value [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, return the leftmost value in the last row of the tree.https://blog.csdn.net/qq_15711195/article/details/126331850LeetCode-538. Convert BST to Greater Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.https://blog.csdn.net/qq_15711195/article/details/126332103LeetCode-235. Lowest Common Ancestor of a Binary Search Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.https://blog.csdn.net/qq_15711195/article/details/122254051LeetCode-236. Lowest Common Ancestor of a Binary Tree [C++][Java]_贫道绝缘子的博客-CSDN博客Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.https://blog.csdn.net/qq_15711195/article/details/122254830LeetCode-530. Minimum Absolute Difference in BST [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a Binary Search Tree (BST), returnthe minimum absolute difference between the values of any two different nodes in the tree.https://blog.csdn.net/qq_15711195/article/details/126332933

     

     


     

    LeetCode-109. Convert Sorted List to Binary Search Tree [C++][Java]_贫道绝缘子的博客-CSDN博客

     

    https://blog.csdn.net/qq_15711195/article/details/126339535?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126339535%22%2C%22source%22%3A%22qq_15711195%22%7D

    LeetCode-653. Two Sum IV - Input is a BST [C++][Java]_贫道绝缘子的博客-CSDN博客

     

  • 相关阅读:
    nlp中如何数据增强
    一款跳转警告HTML单页模板源码
    P06 DDL
    Multi-Graph Fusion Networks for Urban Region Embedding
    flink的regular join/window join/interval join/temporal join/lookup join
    Java 编译和反编译
    AI技术产业热点分析
    大模型日报2024-04-17
    Python学习之-分支语句-基础训练
    小样本学习--(1)概论
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126259008
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号