把一个二叉树转换成一棵最右深度树,并且保证前序遍历顺序不变
2014-01-15 14:45
288 查看
Amazon Interview Question SDE1s
Given a binary tree. Modify it in such a way that after modification you can have a preorder traversal of it using only right pointers. During modification you can use right as well as left pointers. Write complete code and dry run it for some test cases左子树转换后的最右叶子节点的右子树是root,(还要把当前节点的左子树设置为null)。
root.right是右子树转换后的头节点。
public static TreeNode transLeft(TreeNode root) { if (root==null) { return null; } TreeNode result = root; if (root.left!=null) { result = transLeft(root.left); TreeNode leftTrans = result; while (leftTrans.right!=null) { leftTrans = leftTrans.right; } leftTrans.right = root; root.left = null; } if (root.right!=null) { TreeNode rightTrans = transLeft(root.right); root.right = rightTrans; } return result; }
相关文章推荐
- 一、 启动4个线程,在控制台中输出数字,保证数字是按照顺序递增,并且没有重复数字,每隔1秒输出一个数字。
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 12.输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
- 1489 求二叉树的先序遍历 1291 数据结构上机测试4.1:二叉树的遍历与应用1【二叉树遍历顺序转换】
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 数据结构把顺序存储的二叉树转换成链式存储及前序,中序,后序遍历
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 将一个数组转换成深度最低的二叉树
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 【面试题】一串数字,经转换后使所有奇数在所有偶数的前面,并且奇数偶数的相对顺序不变。
- 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
- 给定一个二叉树,返回自下而上的顺序遍历其节点值(即从左到右,从叶到根逐级)。
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回