Skip to content

In-order traversal is written as subsequent traversal #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions data_structure/binary_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,18 @@ class Solution:

```Python
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:

s, postorder = [], []
node, last_visit = root, None

def inorderTraversal(self, root: TreeNode) -> List[int]:
s, inorder = [], []
node = root
while len(s) > 0 or node is not None:
if node is not None:
s.append(node)
node = node.left
else:
peek = s[-1]
if peek.right is not None and last_visit != peek.right:
node = peek.right
else:
last_visit = s.pop()
postorder.append(last_visit.val)

return postorder
node = s.pop()
inorder.append(node.val)
node = node.right
return inorder
```

#### [后序非递归](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/)
Expand Down