Tree
二叉树遍历
144. 二叉树的前序遍历
递归写法
|
|
迭代写法
|
|
94. 二叉树的中序遍历
递归写法
|
|
迭代写法
|
|
145. 二叉树的后序遍历
递归写法
|
|
迭代写法
|
|
102. 二叉树的层序遍历
|
|
103. 二叉树的锯齿形层序遍历
|
|
987. 二叉树的垂序遍历
二叉树路径问题
112. 路径总和
|
|
113. 路径总和 II
|
|
437. 路径总和 III
|
|
前缀和思路
|
|
257. 二叉树的所有路径
|
|
543. 二叉树的直径
|
|
124. 二叉树中的最大路径和
|
|
687. 最长同值路径
|
|
129. 求根节点到叶节点数字之和
|
|
863. 二叉树中所有距离为 K 的结点
|
|
988. 从叶结点开始的最小字符串
二叉树构造
105. 从前序与中序遍历序列构造二叉树
利用 hash table 快速定位根节点
|
|
106. 从中序与后序遍历序列构造二叉树
|
|
889. 根据前序和后序遍历构造二叉树
1028. 从先序遍历还原二叉树
114. 二叉树展开为链表
|
|
109. 有序链表转换二叉搜索树
|
|
108. 将有序数组转换为二叉搜索树
|
|
1008. 前序遍历构造二叉搜索树
二叉树序列化
297. 二叉树的序列化与反序列化
449. 序列化和反序列化二叉搜索树
655. 输出二叉树
606. 根据二叉树创建字符串
求二叉树属性
104. 二叉树的最大深度
111. 二叉树的最小深度
404. 左叶子之和
222. 完全二叉树的节点个数
515. 在每个树行中找最大值
513. 找树左下角的值
662. 二叉树最大宽度
199. 二叉树的右视图
236. 二叉树的最近公共祖先
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3.
|
|
637. 二叉树的层平均值
563. 二叉树的坡度
652. 寻找重复的子树
验证二叉树
100. 相同的树
101. 对称二叉树
110. 平衡二叉树
98. 验证二叉搜索树
|
|
958. 二叉树的完全性检验
993. 二叉树的堂兄弟节点
剑指 Offer 26. 树的子结构
二叉树修改
226. 翻转二叉树
617. 合并二叉树
|
|
814. 二叉树剪枝
116. 填充每个节点的下一个右侧节点指针
117. 填充每个节点的下一个右侧节点指针 II
919. 完全二叉树插入器
二叉搜索树
96. 不同的二叉搜索树
99. 恢复二叉搜索树
-
218 The Skyline Problem
- O(nlog(n)) time using line sweeping and map.
-
220 Contains Duplicate III
- Given a number array, find if there are two elements nums[i] and nums[j] i.e. abs(i-j)<=k and (nums[i]-nums[j])<=t.
- Use set::lower_bound. Enumerate window at size K.
-
230 Kth Smallest Element in a BST
- Save count in node to keep log(n) query time.
-
352 Data Stream as Disjoint Intervals
- Given number stream, merge them into intervals.
-
480 Sliding Window Median
- Use cpp multiset.
-
105 Construct Binary Tree from Preorder and Inorder Traversal
- O(n) time and O(n) space, record val’s inorder index.
-
106 Construct Binary Tree from Inorder and Postorder Traversal
-
129 Sum Root to Leaf Numbers
- Each node is a digit, find sum of numbers formed by root-leaf paths.
-
145 Binary Tree Postorder Treversal
- Morris Traversal. Reverse of left-right mirrored pre-order is post-order.
-
156 Binary Tree Upside Down
-
Do the following transformation recursively: (all right nodes are whether leaf with sibling or nullptr)
1 4 2 3 => 5 2 4 5 3 1
-
-
173 Binary Search Tree Iterator
- Average O(1) time and O(h) memory for next() and hasNext().
- Can delete visited nodes, since it’s single directional visiting.
-
250 Count Univalue Subtrees
- Find the number of univalue subtrees, where all nodes are of the same value.
-
255 Verify Preorder Sequence in Binary Search Tree
- O(n) time by iterative traversal.
- O(1) space by mono-stack and reuse input vector. Mono stack saves nodes not visited yet, thus all nodes less than current value must be visited (popped), because they can only be in left trees.
- Preorder sequence: DDDDIIIDDDDIIIIDDD…, keep a lowest bound for popped ones, they’re left subtrees already visited.
-
272 Closest Binary Search Tree Value II
- Given a BST and target value, find k nearest neighbor.
- O(klog(n)) time using stack-based iterative traversal.
- Cannot delete visited nodes, because it’s bi-directional visiting.
-
298 Binary Tree Longest Consecutive Sequence
- Given a binary tree, find the length of longest consecutive sequence going from up to down.
- O(n) time, simple tree DP.
-
314 Binary Tree Vertical Order Traversal
-
Given a binary tree, output its vertical order traversal. For example:
2 3 4 => 1 3 2 5 0 4 6 1 5|0 6 -
Traverse the tree and emit (node, position) pairs. O(n) time can be achieved if using hashmap.
-
-
337 House Robber III
- Rob on a tree, adjacent nodes cannot be robbed.
- O(n) time, typical tree DP.
-
366 Find Leaves of Binary Tree
- Remove leaves of a binary tree step by step.
- Tree DP. for each node calculate it’s shortest path to leaf.
-
426 Convert Binary Search Tree to Sorted Doubly Linked List
- Convert BST to a sorted list, left=prev, right=next.
- Simple divide and conquer.
-
428 Serialize and Deserialize N-ary Tree
- Simple recursion.
-
431 Encode N-ary Tree to Binary Tree
- Left-(first) children and right-sibiling.
-
449 Serialize and Deserialize BST
- Use 1,,-1,,, like preorder traversal string.
-
545 Boundary of Binary Tree
- Maintain isLeftBound and isRightBound in preorder traversal.
-
742 Closest Leaf in a Binary Tree
- Find the value of nearest leaf to some target node.
- Save nearest leaf for nodes on the root-to-target path.
- Convert tree to graph.
-
987 Vertical Order Traversal of a Binary Tree
- Similar to 314.
-
993 Cousins in Binary Tree
- Two nodes are cousins if they are at the same depth but have different parents. Find whether two nodes are cousins.
-
998 Maximum Binary Tree II
- A maximum tree is whose root is alwasy the largest. Given a number array, a max tree can be constructed by recursively pick maximum and build left/right sub arrays. Now append a new number to origin array, insert the number into the origin maximum tree.
参考资料
-
No backlinks found.