BinaryTreePostorderTraversal
BinaryTreePostorderTraversal.java 源码
package datastructure.tree.leetcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* @author roseduan
* @time 2020/9/15 10:07 下午
* @description 二叉树的后序遍历
*/
public class BinaryTreePostorderTraversal {
private List<Integer> res = new ArrayList<>();
/**
* 递归
*/
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) {
return res;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
res.add(root.val);
return res;
}
/**
* 迭代
*/
public List<Integer> postorderTraversal2(TreeNode root) {
LinkedList<Integer> list = new LinkedList<>();
if (root == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
TreeNode cur = stack.pop();
list.addFirst(cur.val);
if (cur.left != null) {
stack.push(cur.left);
}if (cur.right != null) {
stack.push(cur.right);
}
}
return list;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦