面试题19 二叉树的镜像
2016-06-14 00:49
316 查看
题目:
输入一个二叉树,输出该二叉树的镜像输入:
二叉排序树的前序遍历:8 2 4 7 9 10输出:
该二叉树的镜像的前序遍历:8 9 10 2 4 7解题思路:
本质上就是不停的交换二叉树的左子树和右子树,直到树的叶节点或者节点为空。Java代码实现:
public static void main(String[] args) { // TODO Auto-generated method stub TreeNode node=new TreeNode(); node.insert(8); node.insert(9); node.insert(2); node.insert(4); node.insert(7); node.insert(10); node.oneDisplay(node.root); MirrorRecursively(node.root); System.out.println(); node.oneDisplay(node.root); } //递归调用,递归结束的条件是到了叶节点或者该节点为空 public static Node MirrorRecursively(Node pNode){ if(pNode==null ||(pNode.leftChild==null && pNode.rightChild==null)){ return null; } Node temp=null; temp=pNode.leftChild; pNode.leftChild=pNode.rightChild; pNode.rightChild=temp; MirrorRecursively(pNode.leftChild); MirrorRecursively(pNode.rightChild); return pNode; }
相关文章推荐
- 单链表面试题系列之约瑟夫环
- 面试题18 树的子结构
- 六招让员工职业发展体系落地并发挥价值
- 写给即将步入职场的自己
- 软件测试必须要知道十个关键点
- 程序员的心理修养----->生活篇
- Java程序员的日常 —— 响应式导航Demo
- java多线程面试集锦
- 来自“Java中国”优秀的程序员不会觉得累成狗是一种荣耀
- 面试题17 合并两个排序链表
- leetcode 209. Minimum Size Subarray Sum-子序列最小长度|双指针|二分查找
- 使用STAR方法完善简历
- 《程序员的呐喊》读书笔记(一)
- 程序员面试金典题解 清除行列
- 面试热点之数组
- web浏览器兼容性问题
- 程序员面试金典题解 空格替换
- 多线程面试题
- 面试问题
- 程序员面试金典题解 确定两串乱序同构