二叉树的镜像java实现
2017-09-15 11:30
435 查看
操作给定的二叉树,将其变换为源二叉树的镜像。
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ import java.util.*; public class Solution { public void Mirror(TreeNode root) { if(root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.empty()) { TreeNode node = stack.pop(); if(node.left != null || node.right != null) { TreeNode nodeLeft = node.left; TreeNode nodeRight = node.right; node.left = nodeRight; node.right = nodeLeft; } if(node.left != null) stack.push(node.left); if(node.right != null) stack.push(node.right); } } }
相关文章推荐
- Java实现二叉树镜像 变换
- 剑指offer--面试题19:二叉树的镜像--Java实现
- 笔试面试算法经典--二叉树的镜像-递归与非递归实现(Java)
- 剑指Offer 面试题27:二叉树的镜像 Java代码实现
- 剑指Offer:面试题19——二叉树的镜像(java实现)
- 镜像二叉树并层序打印 Java实现
- 【剑指offer——JAVA实现】二叉树的镜像
- 剑指offer---二叉树的镜像---java实现
- 数据结构与算法分析笔记与总结(java实现)--二叉树12:二叉树的镜像
- java实现操作给定的二叉树,将其变换为源二叉树的镜像。
- 二叉树的镜像(Java实现)
- java实现B树(二叉树)插入,删除
- java实现二叉树
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- java实现二叉树的常见操作
- 二叉树Java实现
- java实现二叉树的建立及遍历方法
- java实现二叉树
- 二叉树Java实现
- 用Java实现二叉树