1.输出二叉树的右视图
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 求二叉树的右视图
* @param preOrder int整型一维数组 先序遍历
* @param inOrder int整型一维数组 中序遍历
* @return int整型一维数组
*/
public int[] solve (int[] preOrder, int[] inOrder) {
// write code here
List<Integer> list = new ArrayList<>();
TreeNode root = createOneTree(preOrder, inOrder, 0, preOrder.length - 1, 0);
layerOrder(root, list);
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
public TreeNode createOneTree(int[] preOrder, int[] inOrder, int left,
int right, int preIndex) {
if (left > right) return null;
if (left == right) return new TreeNode(preOrder[preIndex]);
int item = preOrder[preIndex];
TreeNode node = new TreeNode(preOrder[preIndex]);
for(int i=left;i<=right;i++){
if(inOrder[i]==item){
node.left=createOneTree(preOrder,inOrder,left,i-1,preIndex+1);
node.right=createOneTree(preOrder,inOrder,i+1,right,preIndex+(i-left+1));
break;
}
}
return node;
}
public void layerOrder(TreeNode root, List<Integer> list) {
if (root == null) return;
List<TreeNode> nodeList = new ArrayList<>();
nodeList.add(root);
while (!nodeList.isEmpty()){
int size=nodeList.size();
list.add(nodeList.get(nodeList.size()-1).val);
for(int i=0;i<size;i++){
TreeNode remove = nodeList.remove(0);
if(remove.left!=null) nodeList.add(remove.left);
if(remove.right!=null) nodeList.add(remove.right);
}
}
}
}
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
2.重建二叉树
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param preOrder int整型一维数组
* @param vinOrder int整型一维数组
* @return TreeNode类
*/
public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) {
// write code here
return createTree(preOrder,vinOrder,0, preOrder.length-1,0);
}
public TreeNode createTree(int[] preOrder, int[] vinOrder,int left,int right,int preIndex){
if(left>right) return null;
else if (left == right) {
return new TreeNode(vinOrder[left]);
}else{
int item=preOrder[preIndex];
int mid=0;
for(int i=left;i<=right;i++){
if(vinOrder[i]==item){
mid=i;
}
}
//1 2 3 4 5
//4 2 5 1 3
TreeNode treeNode=new TreeNode(item);
treeNode.left=createTree(preOrder,vinOrder,left,mid-1,preIndex+1);
treeNode.right=createTree(preOrder,vinOrder,mid+1,right,preIndex+(mid-left+1));
return treeNode;
}
}
}
3.在二叉树中找到两个节点的最近公共祖先
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param o1 int整型
* @param o2 int整型
* @return int整型
*/
public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
// write code here
if (root == null) {
return 0;
}
if (root.val == o1) return root.val;
if (root.val == o2) return root.val;
int left = lowestCommonAncestor(root.left, o1, o2);
int right = lowestCommonAncestor(root.right, o1, o2);
if (left != 0 && right != 0) return root.val;
if (left != 0 && right == 0) return left;
if (left == 0 && right != 0) return right;
return 0;
}
}