二叉树习题:
0.创建二叉树节点
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
1.题目描述:
根据一棵树的前序遍历与中序遍历构造二叉树。
如给定: 前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
class Solution {
public int preIndex = 0;
public TreeNode buildTreeChild(int[] preorder, int[] inorder,int inbegin,int inend) {
if(inbegin > inend){
return null;
}
TreeNode root = new TreeNode(preorder[preIndex]);
int index = findValInorder(inorder,preorder[preIndex],inbegin,inend);
preIndex++;
root.left = buildTreeChild(preorder,inorder,inbegin,index - 1);
root.right = buildTreeChild(preorder,inorder,index + 1,inend);
return root;
}
public int findValInorder(int[] inorder,int key,int inbegin,int inend){
for(int i = inbegin;i <= inend;i++){
if(inorder[i] == key){
return i;
}
}
return -1;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || inorder == null){
return null;
}
if(preorder.length == 0 || inorder.length == 0){
return null;
}
return buildTreeChild(preorder,inorder,0,inorder.length - 1);
}
}
2.题目描述:
根据一棵树的中序遍历与后序遍历构造二叉树。
如给定:中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
class Solution {
public int postIndex = 0;
public TreeNode buildTreeChild(int[] inorder, int[] postorder,int postbegin, int postend) {
if(postbegin > postend){
return null;
}
TreeNode root = new TreeNode(postorder[postIndex]);
int index = finValPostorder(inorder,postorder[postIndex],postbegin,postend);
postIndex--;
root.right = buildTreeChild(inorder,postorder,index + 1,postend);
root.left = buildTreeChild(inorder,postorder,postbegin,index - 1);
return root;
}
public int finValPostorder(int[] inorder,int key,int postbegin,int postend){
for(int i = postbegin;i <= postend;i++){
if(inorder[i] == key){
return i;
}
}
return -1;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder == null || postorder == null){
return null;
}
if(inorder.length == 0 || postorder.length == 0){
return null;
}
postIndex = postorder.length - 1;
return buildTreeChild(inorder,postorder,0,inorder.length - 1);
}
}
3.题目描述:
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。
例如:如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,空格字符代表空树。
建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
import java.util.*;
class TreeNode{
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val){
this.val = val;
}
}
public class Main{
public static int i = 0;
//1.建立二叉树
public static TreeNode createTree(String str) {
if(str == null && str.length() <= 0){
return null;
}
TreeNode root = null;
if(str.charAt(i) != '#'){
root = new TreeNode(str.charAt(i));
i++;
root.left = createTree(str);
root.right = createTree(str);
}else{
i++;
}
return root;
}
//2.中序遍历
public static void inOrderTraversal(TreeNode root){
if(root == null){
return ;
}
inOrderTraversal(root.left);
System.out.print(root.val +" ");
inOrderTraversal(root.right);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
TreeNode root = createTree(str);
inOrderTraversal(root);
}
}
4.题目描述:
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){
return null;
}
if(root == p||root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left,p,q);
TreeNode right = lowestCommonAncestor(root.right,p,q);
if(left != null && right != null){
return root;
}else if(left != null){
return left;
}else if(right!= null){
return right;
}
return null;
}
}
5.题目描述:
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。
public class Solution {
public TreeNode prev = null;
public void convertChild(TreeNode pCur) {
if(pCur == null) return;
convertChild(pCur.left);
pCur.left = prev;
if(prev != null){
prev.right = pCur;
}
prev = pCur;
convertChild(pCur.right);
}
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null){
return null;
}
convertChild(pRootOfTree);
TreeNode head = pRootOfTree;
while(head.left != null){
head = head.left;
}
return head;
}
}
6.题目描述:
采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 "()" 表示。而且需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void tree2strChild(TreeNode t,StringBuilder sb) {
if(t == null){
return;
}
sb.append(t.val);
if(t.left == null){
if(t.right == null){
return;
}else{
sb.append("()");
}
}else{
sb.append("(");
tree2strChild(t.left,sb);
sb.append(")");
}
if(t.right == null){
return;
}else{
sb.append("(");
tree2strChild(t.right,sb);
sb.append(")");
}
}
public String tree2str(TreeNode t) {
StringBuilder sb = new StringBuilder();
tree2strChild(t,sb);
return sb.toString();
}
}