💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
### 前序遍历-非递归 ![](https://img.kancloud.cn/eb/d6/ebd6a30c9502f2c242488db7c208e7b0_896x722.png) ~~~ // 前序遍历,非递归 public static void pre(TreeNode node){ Stack<TreeNode> stack = new Stack<>(); // 头节点入栈 stack.push(node); while(!stack.isEmpty()){ TreeNode cur = stack.pop(); System.out.print(cur.val+"->"); // 先压右,再压左 if(null != cur.right){ stack.push(cur.right); } if(null != cur.left){ stack.push(cur.left); } } } ~~~ ``` 前序遍历:(非递归) 1->2->4->5->3->6->7-> ```