Tree
Key points
Traversal:
pre-order, in-order, post-order
Two approaches: 1) DFS, 2) Iteration
Divide & Conquer VS Traversal:
DC: one send left & right two people to get the result, and then do sth.
T: one with a notebook walk through the tree
BST, Binary Search Tree:
in-order is a sorted list, important !
left < root < right, not duplicate
use
TreeSetorTreeMap
Tricks:
isLeaf(): root.left == null && root.right == null
every node: 1)root == null 2) root.left == null 3)root.right == null
Binary Indexed Tree:
the sum of (0, i), in O(logn) time.
Segment Tree:
sum/minimum in a range
Traverse

Pre-order
https://leetcode.com/problems/binary-tree-preorder-traversal/
Iterative
Recursive
In-order
https://leetcode.com/problems/binary-tree-inorder-traversal/
Iterative
Recursive
Post-order
https://leetcode.com/problems/binary-tree-postorder-traversal/
Iterative
Recursive
Reference: https://discuss.leetcode.com/topic/30632/preorder-inorder-and-postorder-iteratively-summarization
Serialization and deserialization
https://leetcode.com/problems/find-duplicate-subtrees https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/ https://leetcode.com/problems/serialize-and-deserialize-bst https://leetcode.com/problems/construct-string-from-binary-tree https://leetcode.com/problems/construct-binary-tree-from-string
DFS or BFS?
BFS: level
DFS: go through a path
Common law
Array -> Complete Binary Tree: parent i, left 2i+1, right 2i+2
Types of Binary Tree
Full Binary Tree
Complete Binary Tree

Binary Search Tree:
left < root < rightuse
TreeSetto implement BST: https://leetcode.com/problems/contains-duplicate-iii.TreeSetis implemented by Black-Red Tree, self balanced.insert,delete,searchinO(logN)complexity.

AVL Tree:
|left height - right height| <= 1

Heap: Max Heap, Min Heap. Heap is complete binary tree.
Priority Queue is a Heap, implemented using an Array object[]. More details about heapify: https://jaywinhuang.gitbooks.io/leetcode/content/mySolutions/023-merge-k-sorted-lists.html 
Segment Tree
the leaves are values of nums

https://leetcode.com/problems/range-sum-query-mutable/description/
ac2: Segment Tree
more intuitive, yet more code. Notice: use divide and conquer, no need to care about the mid point unless building the tree.
Binary Indexed Tree




https://leetcode.com/problems/range-sum-query-mutable/description/ https://leetcode.com/problems/range-sum-query-2d-mutable/description/
ac1: Binary Indexed Tree
careful: bit.length = nums.length + 1; so need to do i++ in methods
https://leetcode.com/problems/reverse-pairs/description/ https://leetcode.com/problems/count-of-smaller-numbers-after-self https://leetcode.com/problems/count-of-range-sum
Binary Search Tree
in-order asecending https://leetcode.com/problems/validate-binary-search-tree/description/ https://leetcode.com/problems/kth-smallest-element-in-a-bst/
Divide & Conquer
get result from children, then do something
https://leetcode.com/problems/longest-univalue-path/description/ https://leetcode.com/problems/count-univalue-subtrees/description/ https://leetcode.com/problems/subtree-of-another-tree/description/
Last updated
Was this helpful?