leetcode刷题笔记Tree专题
Tree
N叉树遍历总结
后序遍历
```python3
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if root == None: #如果根节点不存在
return [] #返回空数组
stack , out = [root,],[] #创建stack(栈)以及out数组
while stack: #stack数组存在节点
root = stack.pop() #将栈顶节点取出赋给根节点
if root: #如果根节点存在
out.append(root.val) #将根节点的值加入到out数组中
for c in root.children: #遍历根节点下的孩子节点
stack.append(c) #将遍历的孩子节点加入到栈中
return out[::-1] #返回逆序out数组
236.二叉树的最近公共祖先
class Solution:
def lowestCommonAncestor(self