![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
二叉树
HeisenbergWDG
努力学习中
展开
-
NC45 实现二叉树先序,中序和后序遍历
实现二叉树先序,中序和后序遍历_牛客题霸_牛客网 (nowcoder.com) /** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root of binary tree * @return int整原创 2021-09-08 22:11:50 · 156 阅读 · 0 评论 -
450. Delete Node in a BST二叉搜索树删除一个节点(巧妙的递归)
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the node原创 2021-03-14 15:50:21 · 337 阅读 · 0 评论 -
230. Kth Smallest Element in a BST二叉搜索树的第k小节点
Given therootof a binary search tree, and an integerk, returnthekth(1-indexed)smallest element in the tree. Example 1: Input: root = [3,1,4,null,2], k = 1 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 Output: 3 ...原创 2021-03-14 15:37:13 · 119 阅读 · 0 评论 -
226. Invert Binary Tree二叉树镜像
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 题目链接:https://leetcode.com/problems/invert-binary-tree/ /** * Definition for a binary tree node. *.原创 2021-03-14 15:30:34 · 149 阅读 · 0 评论 -
437. Path Sum III
You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to chil原创 2021-03-12 16:27:06 · 83 阅读 · 0 评论 -
257. Binary Tree Paths找出所有的路径
Given a binary tree, return all root-to-leaf paths. Note:A leaf is a node with no children. Example: Input: 1 / \ 2 3 \ 5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3 题目链接:https:.原创 2021-03-12 15:29:33 · 104 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree最小祖先节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodespandqas the lowest node inTthat has bothpandqas d...原创 2021-03-12 12:30:00 · 78 阅读 · 0 评论 -
能否由先序遍历和后序遍重建二叉树?
https://blog.csdn.net/qq_42214953/article/details/106286561 答:不能,因为当树的节点存在单个叶子节点的时候,是不能确定树的结构的。 举个例子: 假如一棵树的先序遍历为:6,7,后续遍历为7,6,那么如下图两个结构都是满足条件的。 ...转载 2021-03-10 15:31:05 · 477 阅读 · 0 评论 -
B树、B-树、B+树、B*树
B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right); 2.所有结点存储一个关键字; 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树;如图: B树的搜索,从根结点开始,如果查询的关键字与结点的关键字相等,那么就命中; 否则,如果查询关键字比结点关键字小,就进入左儿子;如果比结点关键字大,就进入 右儿子;如果左儿子或右儿子的指针为空,则报告找不到相应的关键字; 如果B树的所...转载 2021-03-05 09:52:59 · 239 阅读 · 1 评论 -
1022. Sum of Root To Leaf Binary Numbers
You are given therootof a binary tree where each node has a value0or1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is0 -> 1 -> 1 -> 0 -> 1, then this could represent...原创 2020-12-17 20:16:01 · 98 阅读 · 0 评论 -
965. Univalued Binary Tree
A binary tree isunivaluedif every node in the tree has the same value. Returntrueif and only if the given tree is univalued. Example 1: Input: [1,1,1,1,1,null,1] Output: true Example 2: Input: [2,2,2,5,2] Output: false Note: The nu...原创 2020-12-16 20:32:50 · 82 阅读 · 0 评论 -
872. Leaf-Similar Trees
Consider all the leaves of a binary tree, fromleft to right order, the values of thoseleaves form aleaf value sequence. For example, in the given tree above, the leaf value sequence is(6, 7, 4, 9, 8). Two binary trees are consideredleaf-similarif...原创 2020-12-15 20:07:42 · 152 阅读 · 0 评论 -
700. Search in a Binary Search Tree
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL. For example, .原创 2020-12-15 19:51:22 · 89 阅读 · 0 评论 -
589. N-ary Tree Preorder Traversal前序遍历
Given an n-ary tree, return thepreordertraversal of its nodes' values. Nary-Tree input serializationis represented in their level order traversal, each group of children is separated by the null value (See examples). Follow up: Recursive solution ...原创 2020-12-13 20:46:54 · 89 阅读 · 0 评论 -
429. N-ary Tree Level Order Traversal层次遍历
Given an n-ary tree, return thelevel ordertraversal of its nodes' values. Nary-Tree input serializationis represented in their level order traversal, each group of children is separated by the null value (See examples). Example 1: Input: root =...原创 2020-12-10 19:40:51 · 177 阅读 · 1 评论 -
HDU2527哈夫曼树(优先队列和建立哈夫曼树两种方法)
Problem Description Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一样的!并且当储存空间大于一定的值的时候是不安全的!所以Javac++ 就想是否有一种方式是可以得到字符编码最小的空间值!显然这是可以的,因为书上有这一块内容--哈夫曼编码(Huffman Coding);一个字母的权值等于该字母在字符串中出现的频率。所以Javac++ 想让你帮忙,给你安全数值和一串字符串,并让你判断这个原创 2020-05-13 16:07:37 · 512 阅读 · 0 评论 -
leetcode 96. Unique Binary Search Trees BST=左子树*右子树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 ...原创 2019-02-04 12:23:59 · 148 阅读 · 0 评论 -
Leetcode 118. Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output:...原创 2019-02-11 12:32:59 · 214 阅读 · 0 评论 -
Leetcode 129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota...原创 2019-02-13 14:20:28 · 104 阅读 · 0 评论 -
Leetcode 144. Binary Tree Preorder Traversal二叉树前序遍历
Given a binary tree, return thepreordertraversal of its nodes' values. Example: Input:[1,null,2,3] 1 \ 2 / 3 Output:[1,2,3] 题目链接:https://leetcode.com/problems/binary-tree-p...原创 2019-02-20 21:41:06 · 172 阅读 · 0 评论 -
Leetcode 145. Binary Tree Postorder Traversal二叉树后序遍历
Given a binary tree, return thepostordertraversal of its nodes' values. Example: Input:[1,null,2,3] 1 \ 2 / 3 Output:[3,2,1] 题目链接:https://leetcode.com/problems/binary-tree-...原创 2019-02-20 21:44:18 · 188 阅读 · 0 评论 -
Leetcode 199. Binary Tree Right Side View二叉树层次遍历
Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom. Example: Input:[1,2,3,null,5,null,4] Output:[1, 3, 4...原创 2019-03-14 14:33:35 · 138 阅读 · 0 评论 -
Leetcode 117. Populating Next Right Pointers in Each Node II
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right...原创 2019-02-11 12:20:51 · 269 阅读 · 2 评论 -
Leetcode 116. Populating Next Right Pointers in Each Node灵活使用层次遍历
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right...原创 2019-02-11 12:16:19 · 219 阅读 · 0 评论 -
Leetcode 103. Binary Tree Zigzag Level Order Traversal Z字型层次遍历
Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tre...原创 2019-02-09 16:02:20 · 214 阅读 · 0 评论 -
Leetcode 102. Binary Tree Level Order Traversal层次遍历
Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree[3,9,20,null,null,15,7], 3 / \ 9 20 ...原创 2019-02-09 15:43:31 · 179 阅读 · 0 评论 -
Leetcode 101. Symmetric Tree对称树判断
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree[1,2,2,3,4,4,3]is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 ...原创 2019-02-09 10:34:10 · 150 阅读 · 0 评论 -
leetcode 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 are structurally identical and the nodes have the same value. Example ...原创 2019-02-04 15:09:30 · 224 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree合法的二叉搜索树BST 完全二叉树判断
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keysless thanthe node's key. The ...原创 2019-02-04 14:16:25 · 212 阅读 · 0 评论 -
Leetcode 95. Unique Binary Search Trees II BST二叉搜索树
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n. Example: Input: 3 Output: [ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3...原创 2019-02-04 11:44:56 · 205 阅读 · 0 评论 -
Leetcode 94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return theinordertraversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] 题目链接:https://leetcode.com/problems/binary-tree-in...原创 2019-02-03 16:37:12 · 164 阅读 · 0 评论 -
HDU3999前序遍历
Problem Description As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely: 1. insert a key k to a empty tree, then the tree become a tree wi...原创 2018-07-28 18:11:25 · 258 阅读 · 0 评论 -
HDU1710二叉树后序遍历
Problem Description A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most impor...原创 2018-07-28 17:15:28 · 409 阅读 · 0 评论 -
leetcode 104. Maximum Depth of Binary Tree求二叉树深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note:A leaf is a node with no childre...原创 2019-02-09 16:10:18 · 157 阅读 · 0 评论 -
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal前序,中序遍历序列重建二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder =[3,9,20,15,7] inorder = [9,3...原创 2019-02-09 17:33:18 · 186 阅读 · 0 评论 -
Leetcode 114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ ...原创 2019-02-11 12:01:53 · 123 阅读 · 0 评论 -
Leetcode 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note:A leaf is a node with no children. Example: Given the below binary tree andsum = 22, ...原创 2019-02-10 15:41:24 · 127 阅读 · 0 评论 -
Leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note:A leaf is a node with no children. Example:...原创 2019-02-10 15:32:46 · 210 阅读 · 0 评论 -
Leetcode 110. Balanced Binary Tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees ofeverynode never diff...原创 2019-02-10 15:21:56 · 192 阅读 · 0 评论 -
leetcode 111. Minimum Depth of Binary Tree二叉树最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note:A leaf is a node with no childre...原创 2019-02-10 14:24:44 · 193 阅读 · 0 评论