企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
### 后续遍历-非递归 ![](https://img.kancloud.cn/eb/d6/ebd6a30c9502f2c242488db7c208e7b0_896x722.png) ~~~ // 后序遍历,非递归 public static void back(TreeNode node){ Stack<TreeNode> stack = new Stack<>(); Stack<TreeNode> collect = new Stack<>(); // 头节点入栈 stack.push(node); while(!stack.isEmpty()){ TreeNode cur = stack.pop(); // 弹出的元素放到收集栈内 collect.push(cur); // 先压左再压右 if(null != cur.left){ stack.push(cur.left); } if(null != cur.right){ stack.push(cur.right); } } // 最后弹出收集栈里的节点 while(!collect.isEmpty()){ System.out.print(collect.pop().val+"->"); } } ~~~ ``` 中序遍历:(非递归) 4->5->2->6->7->3->1-> ```