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...
Steven Lynn
Steven Lynn
喂马、劈柴、周游世界
最新发布
我与 Dify 的半年
2025-3-9
我的2022年终小结
2024-11-9
记录雅思考试经历与一点学习心得
2024-11-9
Hackergame 2024 思路小结
2024-11-9
黑客松、日本、入职:我的2024下半年的总结
2024-11-9
NotionNext:基于Notion和NextJS的开源博客
2024-11-9