🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 中序遍历-非递归 ![](https://img.kancloud.cn/eb/d6/ebd6a30c9502f2c242488db7c208e7b0_896x722.png) ~~~ // 中序遍历,非递归 public static void in(TreeNode node){ Stack<TreeNode> stack = new Stack<>(); // 头节点入栈 stack.push(node); while(!stack.isEmpty()){ // 左子数入栈 while(node.left != null){ stack.push(node.left); node = node.left; } TreeNode cur = stack.pop(); System.out.print(cur.val+"->"); // 如果有右,则右节点入栈 if(null != cur.right){ stack.push(cur.right); // 将node指针指向右节点 node = cur.right; } } } ~~~ ``` 中序遍历:(非递归) 4->2->5->1->6->3->7-> ```