的最大深度时,这是一个寻找二叉树的最大深度的伪代码: maxDepth(Node N)
1. If Nodes is leaf node then return 0
2. Else
(a) Get the max depth of left subtree recursively i.e.,
call maxDepth(N->left-subtree)
我试图打印二叉树的所有路径(根到叶路径),但无济于事。 我的策略是使用递归,其基本情况为either tree is None or tree node is leaf return否则,遍历树的左侧和右侧。 但我找不到保留左右树的方法。 def pathSum(self, root, target, result):
if not root:
return []