当前位置: 首页> 娱乐> 八卦 > 2023网页游戏大全_制作公司网站教程_广告投放平台系统_怎么在百度上发广告

2023网页游戏大全_制作公司网站教程_广告投放平台系统_怎么在百度上发广告

时间:2025/7/24 19:02:25来源:https://blog.csdn.net/m0_51275144/article/details/145539630 浏览次数:0次
2023网页游戏大全_制作公司网站教程_广告投放平台系统_怎么在百度上发广告

二叉树前中后遍历(迭代遍历)

前序遍历
public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) return res;Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode node = stack.pop();res.add(node.val);//先右后左if (node.right != null) stack.push(node.right);if (node.left != null) stack.push(node.left);}return res;
}
中序遍历
public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) return res;Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while (cur != null || !stack.isEmpty()) {//找最左边的节点while (cur != null) {stack.push(cur);cur = cur.left;}//弹出栈顶,访问cur = stack.pop();res.add(cur.val);//找右节点cur = cur.right;}return res;
}
后序遍历
//后序遍历相当于先序遍历的逆序
public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) return res;Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode cur = stack.pop();res.add(cur.val);if (cur.left != null) stack.push(cur.left);if (cur.right != null) stack.push(cur.right);}// 反转return res.reversed();
}
关键字:2023网页游戏大全_制作公司网站教程_广告投放平台系统_怎么在百度上发广告

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: