Steven Lynn’s Blog

Thought from traverse a binary tree

2024-4-2654 words3 min read
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
  1. print to arr.append(root.val) , space
  1. 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
 
Loading...