Given a binary tree, return the postorder traversal of its nodes’ values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [3,2,1]
二叉树的后序遍历
思路:
后序遍历
//0ms
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
postTraversal(root, result);
return result;
}
public void postTraversal(TreeNode root, List<Integer> result) {
if (root == null) {
return;
}
postTraversal(root.left, result);
postTraversal(root.right, result);
result.add(root.val);
}
本文介绍了一种实现二叉树后序遍历的方法,通过递归算法,先遍历左子树,再遍历右子树,最后访问根节点,返回节点值的后序遍历序列。
296

被折叠的 条评论
为什么被折叠?



