/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> als = new ArrayList<Integer>();
if(root == null){
return als;
}
Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>();
TreeNode iter = root;
s1.push(iter);
while(!s1.isEmpty()){
TreeNode head = s1.pop();
s2.push(head);
if(head.left != null){
s1.push(head.left);
}
if(head.right != null){
s1.push(head.right);
}
}
while(!s2.isEmpty()){
als.add(s2.pop().val);
}
return als;
}
}
非递归的方式后序遍历二叉树
最新推荐文章于 2024-07-17 12:11:36 发布