![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode Tree
Tree
Tech In Pieces
这个作者很懒,什么都没留下…
展开
-
LeetCode Tree类问题注意的点总结
首先最最基本的就是前序后序中序层次遍历。这个要闭着眼睛都能写出来其递归形式和遍历形式。递归形式是我们非常熟悉的 利用helper辅助函数,进行递归 递归函数非常简单 只需要注意每次add放置的位置即可。而且要注意对于root是否为null的判断。唯一需要强调的是层次遍历,我们的递归函数有三个变量,第一个是二维数组,第二个是输入的root, 第三个是level 我们采取夹心涂层的方式进行写入(为什...原创 2020-12-11 08:00:58 · 181 阅读 · 0 评论 -
LeetCode 147. Insertion Sort List
//利用插入排序的思想对一个链表进行排序class Solution { public ListNode insertionSortList(ListNode head) { if(head == null || head.next == null) return head; ListNode dummy = new ListNode(0); dummy.next = head; //ListNod原创 2020-11-20 03:37:11 · 58 阅读 · 0 评论 -
LeetCode链表 常见的题型以及要注意的点
这是一类很特殊的题目 看到这种题目 不要试图自己去想怎么做 要想一想之前是不是遇到过类似的题目 如果没有的话然后再好好想想技巧以及要注意的点之类的。注意链表的遍历用while而不是for由于大多数链表的题目都很简单 因此每个题目的后面都用一句话总结一下要注意的点Basic:206 链表反转 use dummy node at head, and use four step swap technique in linkedlist92 链表反转2 反转部分链表 范围从m到n 和206没什么区别14原创 2020-11-20 03:35:16 · 186 阅读 · 0 评论 -
LeetCode树 常见的题型以及要注意的点
要注意的点:注意题目说的是binary tree, binary search tree, 还是n-ary tree.原创 2020-11-19 04:08:42 · 171 阅读 · 0 评论 -
LeetCode 117. Populating Next Right Pointers in Each Node 2
follow up of LC116. we used to have a full binary tree but not now we don’t have.我们之前是只用两个指针 现在用三个指针.class Solution { public Node connect(Node root) { Node head = null; Node pre = null; Node cur = root; while(cur != nu原创 2020-11-19 04:07:58 · 119 阅读 · 0 评论 -
LeetCode 116. Populating Next Right Pointers in Each Node
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;}Populate each next pointer to point t原创 2020-11-19 03:27:40 · 112 阅读 · 0 评论 -
LeetCode 114. Flatten Binary Tree to Linked List?
Given a binary tree, flatten it to a linked list in-place.although it looks kind of simple but don’t even know how to implement that.it is preorder traverse with a little modification.class Solution { public void flatten(TreeNode root) { if原创 2020-11-19 02:50:52 · 53 阅读 · 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 total sum of all root-to-leaf numbers.example:Input: [1,2,3]原创 2020-11-19 02:01:17 · 54 阅读 · 0 评论 -
LeetCode 124. Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.For this problem****, a path is defined as any node sequence from some starting node to any node in the tree along the parent-child connections.**** The path must contain at least one node and does原创 2020-11-19 00:59:31 · 629 阅读 · 1 评论 -
LeetCode 1367. Linked List in Binary Tree
Given a binary tree and a linkedlist, check if there is a path or subpath(downward) in this tree which contains the same value as given linkedlist.and pay attention: the binary tree nodes may contains some duplicate values.idea:the first thing come to m原创 2020-11-18 12:01:31 · 147 阅读 · 0 评论 -
LeetCode 1372. Longest ZigZag Path in a Binary Tree
Given a binary tree root, a ZigZag path for a binary tree is defined as follow:Choose any node in the binary tree and a direction (right or left).If the current direction is right then move to the right child of the current node otherwise move to the lef原创 2020-11-18 11:08:02 · 122 阅读 · 0 评论 -
LeetCode 1123. Lowest Common Ancestor of Deepest Leaves?
经典LCA的变形题目现在不是要求 你找任意给定的两个Node的LCA了 而是找最深leaves的LCA。回想一下 之前解决这个问题的方法是:通过preorder traverse 来构建一个child-parent的hashmap,然后再从p出发得到所有可能的的LCA 然后从q出发继续检索。找到第一个再LCA pool里面的 那就是真正的LCA。但是我们如何解决这个问题呢?注意:所有节点的值都是Unique的。但是我根本就没明白这个题目到底想干嘛, 如果有多组deepest leaves那么应该怎原创 2020-11-18 10:37:13 · 136 阅读 · 0 评论 -
LeetCode 1161. Maximum Level Sum of a Binary Tree
suppose the level of root is 1.now we need to return the smallest level x that sum of all that node values of that level is the max.of course we can do that using level order traverse and maintain the smallest level and return it. but it not sounds like原创 2020-11-18 03:03:30 · 112 阅读 · 0 评论 -
LeetCode 662. Maximum Width of Binary Tree?
树的width的定义是:每一层的最左和最右之间的额所有Node的数量(包括null node)idea:我们给每一层的节点赋值 比如说root位置为0 那么第一层就分别是-1 和 1,如果他们也同样分别往左或者往右扩的时候变为-2和2我们只需要层次遍历的时候对最左和最右的节点进行更新就行(但是仔细想一下 这实际上是不行的 我们必须要对所有的节点进行位置的维护 因为我们不知道下一层的边界是属于上一层的哪两个父节点的 而这个边界的位置又取决于这两个父节点的位置)基于这个想法 我用以下代码实现 但是效果原创 2020-11-18 01:19:45 · 126 阅读 · 0 评论 -
LeetCode 513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.就是拿到最后一层的第一个元素。这个元素是最左下角的元素,不是最左侧的元素。如果想实现 其实也很简单 就是维护更新每一层的第一个元素。class Solution { public int findBottomLeftValue(TreeNode root) { if (root == null) return -1;原创 2020-11-17 11:52:42 · 142 阅读 · 0 评论 -
LeetCode 236. Lowest Common Ancestor of a Binary Tree
LCA problem, a really classic one.public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)原创 2020-11-17 10:48:40 · 79 阅读 · 0 评论 -
LeetCode 993. Cousins in Binary Tree
still an easy question.cousins are in the same depth but with different parents.now we are given a binary tree which each value is unique.public boolean isCousins(TreeNode root, int x, int y)let’s check if the given two values are cousin or not.let’s原创 2020-11-16 22:50:49 · 109 阅读 · 0 评论 -
LeetCode 671 Second Minimum Node In a Binary Tree?
given a tree which each val of the nodes are positive. output the second minimum node in a binary tree…pay attention: the second minimum should be the value larger than smallest if we can’t find somrthing like this then we return -1;in order to get the s原创 2020-11-16 22:29:04 · 73 阅读 · 0 评论 -
LeetCode 965. Univalued Binary Tree
check if every value on this tree is univalue or not.using recursion and the equation will be:isUnivalue(root) = isUnivalue(root.left) && isUnivalue(root.right)but the problem is we don’t have the value to compare if any one is false.so I use a原创 2020-11-16 11:59:33 · 119 阅读 · 0 评论 -
LeetCode 543 Diameter of Binary Tree
The diameter of a binary tree is the length of the longest path between any two nodes in a tree.so it’s actually needs us to get the longest path in the binary tree, no matter it surpass the root or not.return the length of that path.diameter(root) = Ma原创 2020-11-16 11:43:50 · 111 阅读 · 0 评论 -
LeetCode 958. Check Completeness of a Binary Tree
the definition of complete binary tree is:all the leaf nodes should be as left as it can be. and every other level is full except the bottom node levelit remind me of the recursion way. but after i write the following codes, it’s not workingclass Soluti原创 2020-11-15 04:19:33 · 126 阅读 · 0 评论 -
LeetCode 404 sum of left leaves
Find the sum of all left leaves in a given binary treethink about this, when we reached leaf, how are we gonna to know if it is left leaf or not?of course we can modify the iterate version of preorder traverse, it’s very simple.but how are we gonna do i原创 2020-11-15 02:52:11 · 109 阅读 · 0 评论 -
LeetCode 110 check if balanced binary tree
平衡树的定义:任意节点的左右子节点的高度差不超过1.说到左右子树 就想到递归。我们要check所有的节点是不是符合这个要求。isBalanced(root) = isBalanced(root.left) && isBalanced(root.right)but given a node, how to check if it is or not, we should do this:isBalanced(root) = Math.abs(height(root.left) -原创 2020-11-15 01:30:47 · 102 阅读 · 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.the follow up the LC112.but the difference is: LC112 ask if we can find some path like that then return true.and this problem needs us to return all th原创 2020-11-14 05:59:02 · 114 阅读 · 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.class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return f原创 2020-11-14 04:30:35 · 98 阅读 · 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.判断某条路径的加和值是不是等于给定值。就像是print路径一样class Solution { public boolean hasPathSum(TreeNode root, int sum) {原创 2020-11-14 04:27:48 · 66 阅读 · 0 评论 -
LeetCode 988 smallest string starting from leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters ‘a’ to ‘z’: a value of 0 represents ‘a’, a value of 1 represents ‘b’, and so on.Find the lexicographically smallest string that starts at a leaf of this tree and原创 2020-11-14 03:57:08 · 114 阅读 · 0 评论 -
LeetCode 257. Binary Tree Paths
print all the paths of BT, the out put format should be: [“1->2->5”, “1->3”]clearly, we will use the preorder, but they are not exactly the same.I can’t think of any ways to get this problem done.but based on the solution:binaryTreePath(root)原创 2020-11-13 12:25:58 · 125 阅读 · 0 评论 -
LeetCode 515 find largest value in each tree row
the tree row here means the level order traverse.public List largestValues(TreeNode root)of course we can still use the level order traverse and after we get the whole list, we get the max.but in the mean time, we can also get the largest by maintaining原创 2020-11-13 04:58:32 · 101 阅读 · 0 评论 -
LeetCode 637 average of levels in binary tree
just use level traverse and calculate the average of each level.class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> final_res = new ArrayList<>(); //List<List<Integer>> res =原创 2020-11-13 04:36:35 · 104 阅读 · 0 评论 -
LeetCode 107 binary level order traversal 2
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).idea: Not zigzag level order traversal, but bottom up traversal:the only difference between original level or原创 2020-11-13 04:17:26 · 104 阅读 · 0 评论 -
LeetCode 572 Subtree of another tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a原创 2020-11-13 04:02:49 · 98 阅读 · 0 评论 -
LeetCode 559. Maximum Depth of N-ary Tree
using recursion to solve this problem:maxDepth(root) = Math.max(maxDepth(root.kid1), Math.max(root.kid2)…) + 1;class Solution { public int maxDepth(Node root) { if (root == null) return 0; int height = 0; for (Node child:原创 2020-11-13 03:22:23 · 105 阅读 · 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.This is the problem that can be solved recursion; all we need to do is write the right equation:原创 2020-11-13 03:15:45 · 57 阅读 · 0 评论 -
LeetCode 104. Maximum Depth of Binary Tree
clearly we can use recursion to solve this problem.and the recursion equation will be:maxDepth(root) = Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0;原创 2020-11-13 02:30:36 · 87 阅读 · 0 评论 -
LeetCode 101 symmetric tree
Checking if a given tree is symmetric tree or not.so all we need to do is write the recursion equations:isSymmetric(root) = isSym(root.left, root.right) //two sub tree is symisSym(left, right) = (left.val == right.val) && isSym(left.left, right.原创 2020-11-13 02:02:49 · 104 阅读 · 0 评论 -
LeetCode 100 same tree
public boolean isSameTree(TreeNode p, TreeNode q) { }this is the kind of problem that can use recursion, and all we have to do is write the write equations:isSameTree(p, q) = (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.原创 2020-11-13 01:38:38 · 103 阅读 · 0 评论