leetcode中的:二叉树的层次遍历ii
class Solution: def levelOrderBottom(self, root: TreeNode) -> List[List[int]]: if not root: return None stack1 = [root] stack2 = [] ret = [] while stack1 or stack2: if stack1: tmpList = [] while stack1: tmp = stack1[0] tmpList.append(tmp.val) if tmp.left: stack2.append(tmp.left) if tmp.right: stack2.append(tmp.right) del stack1[0] ret.insert(0, tmpList) if stack2: tmpList = [] while stack2: tmp = stack2[0] tmpList.append(tmp.val) if tmp.left: stack1.append(tmp.left) if tmp.right: stack1.append(tmp.right) del stack2[0] ret.insert(0, tmpList) return ret