💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
![](https://box.kancloud.cn/0a86528382673e1f0cf1450570955ef7_1179x586.png) ~~~ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> ls = new ArrayList<Integer>(); if(root==null) return ls; Stack<TreeNode> st = new Stack<TreeNode>(); HashSet<TreeNode> hs = new HashSet<TreeNode>(); st.push(root); while(!st.isEmpty()) { TreeNode temp = st.pop(); if(hs.contains(temp)) { ls.add(temp.val); continue; } hs.add(temp); st.push(temp); if(temp.right!=null) st.push(temp.right); if(temp.left!=null) st.push(temp.left); } return ls; } } ~~~