剑指offer:(19)面试官谈面试思路:二叉树的镜像
来源:互联网 发布:多开同步器软件 编辑:程序博客网 时间:2024/05/21 13:56
操作给定的二叉树,将其变换为源二叉树的镜像。
二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5
package co.com.jianzhioffer;public class Solution19 {public class TreeNode {int val;TreeNode left = null;TreeNode right = null;TreeNode(int val) {this.val = val;}}public void Mirror(TreeNode root){if(root == null ) return;TreeNode p = root.left;root.left = root.right;root.right = p;if(root.left!=null) Mirror(root.left);if(root.right!=null) Mirror(root.right); }}
import java.util.Stack;/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { public void Mirror(TreeNode root) { if(root == null ) return;Stack<TreeNode> stackNode = new Stack<TreeNode>();stackNode.push(root);while(!stackNode.empty()){TreeNode tree = stackNode.pop();if(tree.left!=null || tree.right != null){TreeNode temp = tree.left;tree.left = tree.right;tree.right = temp;}if(tree.left!=null){stackNode.push(tree.left);}if(tree.right!=null){stackNode.push(tree.right);} } }}
0 0
- 剑指offer:(19)面试官谈面试思路:二叉树的镜像
- 剑指offer 19题 【面试思路】二叉树的镜像
- 剑指offer-4-面试19:二叉树的镜像
- 《剑指offer》刷题笔记(面试思路):二叉树的镜像
- 【剑指Offer面试编程题】题目1521:二叉树的镜像--九度OJ
- 【剑指offer】二叉树的镜像
- [剑指Offer]二叉树的镜像
- 剑指offer--二叉树的镜像
- 剑指Offer-二叉树的镜像
- 《剑指offer》二叉树的镜像
- 剑指 offer:二叉树的镜像
- 【剑指offer】二叉树的镜像
- 剑指offer 二叉树的镜像
- [剑指offer]二叉树的镜像
- 《剑指offer》-二叉树的镜像
- 剑指Offer:二叉树的镜像
- 剑指offer|二叉树的镜像
- 《剑指offer》二叉树的镜像
- IDEA 字体、语法高亮设置
- webservice 简单项目之心得体会
- 最少货币流通, 硬币找钱问题
- mysql更新语句使用order by
- 修改recycleview封装
- 剑指offer:(19)面试官谈面试思路:二叉树的镜像
- 基于gitosis管理git服务器
- 微服务架构Spring Cloud和Dubbo 还有EDAS
- Activity跳转、子线程
- 【Android】二维码识别性能优化
- Linux 用户权限设置和切换用户
- PHP7 操作MongoDB
- 第五届省赛题 Metric Matrice
- Bmob实现登陆注册,解决官方signUp与Success方法更新后出错问题