题目链接:树的子结构 import java.util.*;
/**
public class TreeNode {int val 0;TreeNode left null;TreeNode right null;public TreeNode(int val) {this.val val;}}
*/
public class Solution {public boolean HasSubtree(TreeNode root1,TreeNode root2) …
1102 Invert a Binary Tree (25 分)
The following is from Max Howell twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you cant invert a binary tree on a whiteboard so fuck off.Now it’s your turn to prove that YOU CAN invert…
题目链接:从上往下打印二叉树 import java.util.*;
import java.util.ArrayList;
/**
public class TreeNode {int val 0;TreeNode left null;TreeNode right null;public TreeNode(int val) {this.val val;}}
*/
public class Solution {public ArrayList<I…
题目链接:二叉树中和为某一值的路径 import java.util.*;/** public class TreeNode {* int val 0;* TreeNode left null;* TreeNode right null;* public TreeNode(int val) {* this.val val;* }* }*/public class Solution {/*** 代码中的类名、…
文章目录 一、题目二、题解 一、题目
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums [1,2…
题目链接:二叉树的镜像 import java.util.*;/** public class TreeNode {* int val 0;* TreeNode left null;* TreeNode right null;* public TreeNode(int val) {* this.val val;* }* }*/public class Solution {/*** 代码中的类名、方法名、参数…
文章目录 5.2.1 二叉树二叉树性质引理5.1:二叉树中层数为i的结点至多有 2 i 2^i 2i个,其中 i ≥ 0 i \geq 0 i≥0。引理5.2:高度为k的二叉树中至多有 2 k 1 − 1 2^{k1}-1 2k1−1个结点,其中 k ≥ 0 k \geq 0 k≥0。引理5.3&…
https://www.luogu.com.cn/problem/CF1192B
对于直径的求法,常用dp或两次dfs,但如果要动态维护似乎都不太方面,那么可以维护树上路径最大值。
树上路径为: d e p u d e p v − 2 d e p l c a ( u , v ) dep_udep_v-2\times de…
今天数据结构课小测考了两道关于树的题目,总结下来,顺便学习递归:
leetcode 100
\100. Same Tree
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they …
文章目录 一、题目二、题解 一、题目
A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
Example 1:
Input: root [1,1,1,1,1,null,1] Ou…
题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Each input file contains one test cas…
文章目录 一、题目二、题解 一、题目
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node { int val; Node *left; Node *right; Node *next; } …
【LetMeFly】2477.到达首都的最少油耗:深度优先搜索(DFS)
力扣题目链接:https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital/
给你一棵 n 个节点的树(一个无向、连通、无环图),每个节点表示一…
文章目录 一、题目二、题解 一、题目
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p an…
文章目录 一、题目二、题解 一、题目
Given the root of a binary tree, return the leftmost value in the last row of the tree.
Example 1:
Input: root [2,1,3] Output: 1 Example 2:
Input: root [1,2,3,4,null,5,6,null,null,7] Output: 7
Constraints:
The num…
文章目录 一、题目二、题解 一、题目
Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.
If the tree has more than one mode, return them in any order.
Assume a BST is def…
文章目录 一、题目二、题解 一、题目
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
Example 1:
Input: nums [-10,-3,0,5,9] Output: [0,-3,9,-10,null,5] Explanation: [0,-10,5…
准备二叉树的结构
static class Node{public Node right;public Node left;public int value;public Node(int data){this.value data;}
}
// 给二叉树节点赋值public static void main(String[] args) {Node head new Node(1);head.left new Node(2);head.right new Nod…
文章目录 一、题目二、题解 一、题目
Given the root of a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between).
Example 1:
Input: root [3,9,20,n…
转载自https://blog.csdn.net/u011935772/article/details/71419527 组织机构表结构 建表语句 drop table if exists Organize; /**/ /* Table: Organize */ /**/ create table Organize ( ID smallint not…
文章目录 一、题目二、题解 一、题目
Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
Example 1:
Input: …
有一棵 n n n 个节点的以 1 1 1 号点为根的有根树。现在可以对这棵树进行若干次操作,每一次操作可以选择树上的一个点然后删掉连接这个点和它的儿子的所有边。
现在我们想知道对于每一个 k k k ( 1 ≤ k ≤ n 1≤k≤n 1≤k≤n),最少需要多少次操作能…
题目链接:二叉树的深度 import java.util.*;
/**
public class TreeNode {int val 0;TreeNode left null;TreeNode right null;public TreeNode(int val) {this.val val;}}
*/
public class Solution {public int TreeDepth(TreeNode root) {if(root null) ret…
文章目录 一、题目二、题解 一、题目
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level x such that the sum of all the values of nodes at level x is maximal.
Example 1:
Input:…
文章目录 一、题目二、题解 一、题目
Given a binary tree, determine if it is height-balanced .
Example 1:
Input: root [3,9,20,null,null,15,7] Output: true Example 2:
Input: root [1,2,2,3,3,null,null,4,4] Output: false Example 3:
Input: root [] Output…
文章目录 一、题目二、题解 一、题目
You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:
Create a root node whose value is the maximum value in nums. Recursively bu…
【LetMeFly】2581.统计可能的树根数目:换根DP(树形DP)
力扣题目链接:https://leetcode.cn/problems/count-number-of-possible-root-nodes/
Alice 有一棵 n 个节点的树,节点编号为 0 到 n - 1 。树用一个长度为 n - 1 的二维整数数组 edges…
分数 30
全屏浏览题目
作者 CHEN, Yue
单位 浙江大学
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
The left subtree of a node contains only nodes with keys less than the nodes key.The right subtree…