Find the max of binary treeminsum a treeDetermining whether a tree is a balanced binary tree.Kth smallest/largest element in a BST
Three kinds of traverse binary tree.
change
print
to yield
/ yield from
we can write
Here we find that a complex recursive traversal is turned into a "tiling”
What are the applications?
Find the max of binary tree
min
sum a tree
Space , But it feels like traverse an array.
RIIR
ref:
Determining whether a tree is a balanced binary tree.
Problem solution: mid-order traversal is ascending.
The traditional approach is
print
toarr.append(root.val)
, space
- brute force
ans = max(ans, cur)
in the tree
The former is imperfect, the latter is hard to write.
What if we use
yield
?Kth smallest/largest element in a BST
What are the applications.
For example recursive traversal of a file tree.
Ideas from.
SICP lec6a : stream
Classic problem: Print the longest/and largest paths. (Written without pruning. For clear)
Change a writing style, that is, the pyramid maximum path
but we can just use the dfs result
Rust 通过 tunnel 实现
Checking whether the search binary tree is valid.
even bfs can use recursion