要注意的点:
注意题目说的是binary tree, binary search tree, 还是n-ary tree.
截止到现在 大约有91道tree的题目在leetcode里面
https://darktiantian.github.io/LeetCode%E7%AE%97%E6%B3%95%E9%A2%98%E6%95%B4%E7%90%86%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%E7%AF%87%EF%BC%89Tree/
常见的题型:
Binary/N-ary tree pre/in/post/level order traverse. do it iteratively and recursively.
Max/Min depth of binary tree/N-ary trree
construct BT from Pre/in/post(choose two)
serialize and deserialize binary tree/N-ary tree
其他的就是一些千奇百怪的题目了 如果不是能深入的理解tree的题目该从何处着手 一定会很难受 毕竟变化多端。
100 same tree
101 symmetric tree
104/559 Min/Max depth of binary/N-ary tree
572 Subtree of another tree
107 binary level order traversal 2
637 average of levels in binary tree /515 find largest value in each tree --求层次遍历中的每一层的平均值和最大值
257 print tree path / 988 smallest string starting from leaf / 112 path sum/ 113 path sum 2
110 check if balanced binary tree
404 sum of left leaves
958. Check Completeness of a Binary Tree
959. Diameter of Binary Tree
960. Univalued Binary Tree
961. Second Minimum Node In a Binary Tree
962. Cousins in Binary Tree
236. Lowest Common Ancestor of a Binary Tree
513. Find Bottom Left Tree Value
662. Maximum Width of Binary Tree
222. Count Complete Tree Nodes
1161. Maximum Level Sum of a Binary Tree
1123. Lowest Common Ancestor of Deepest Leaves
1372. Longest ZigZag Path in a Binary Tree
1367. Linked List in Binary Tree
124. Binary Tree Maximum Path Sum
129. Sum Root to Leaf Numbers
114. Flatten Binary Tree to Linked List
116. Populating Next Right Pointers in Each Node
117. Populating Next Right Pointers in Each Node 2
655. Print Binary Tree
307. Range Sum Query - Mutable
要注意的点:
要有题感 能感觉出来这个问题是不是可以用递归去解 想明白我们是用(递归函数的含义是什么?一层递归还是两层递归(helper函数)?递归等式如何去写?)
这道题可以用pre/in/post/level order的变形去解吗?
root == null 判断了吗?当递归到叶子节点的处理情况写了吗?
BST:
1382. Balance a Binary Search Tree
108 convert sorted array to BST
235 Lowest Common Ancestor of a BST(这个算是很高频的题目了吧)
938 range sum of BST
530 Minimum Absolute Difference in BST / 783. Minimum Distance Between BST Nodes
653.Two Sum IV - Input is a BST
963. Search in a BST
964. Kth Smallest Element in a BST
98. Validate Binary Search Tree
109. Convert Sorted List to Binary Search Tree
450. Delete Node in a BST
1373. Maximum Sum BST in Binary Tree
501. Find Mode in Binary Search Tree