1. 二叉树的构建及遍历。
2. 二叉树的分层遍历 。
3. 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先 。
4. 二叉树搜索树转换成排序双向链表。
5. 根据一棵树的前序遍历与中序遍历构造二叉树。
6. 根据一棵树的中序遍历与后序遍历构造二叉树
7. 二叉树创建字符串。
package tree;
import sun.font.CreatedFontTracker;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class TreeNode {
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
public class OfferAdvanceTree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.nextLine();
TreeNode root = build(str);
inOrder(root);
System.out.println();
}
System.out.println("下面是build1:");
TreeNode t = build1();
inOrder(t);
System.out.println("--------");
Solution s = new Solution();
System.out.println(s.levelOrder(t).toString());
}
public static int index = 0;
public static TreeNode build(String str) {
index = 0;
return creatTreePrevOrede(str);
}
private static TreeNode creatTreePrevOrede(String str) {
char c = str.charAt(index);
if (c == '#') {
return null;
}
TreeNode treeNode = new TreeNode(c);
index++;
treeNode.left = creatTreePrevOrede(str);
index++;
treeNode.right = creatTreePrevOrede(str);
return treeNode;
}
public static void inOrder(TreeNode root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val);
inOrder(root.right);
}
//给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
public static TreeNode build1() {
TreeNode a = new TreeNode('A');
TreeNode b = new TreeNode('B');
TreeNode c = new TreeNode('C');
TreeNode d = new TreeNode('D');
TreeNode e = new TreeNode('E');
TreeNode f = new TreeNode('F');
TreeNode g = new TreeNode('G