ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 前序遍历-非递归 ![](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-> ```