面試算法代碼知識梳理系列
- 插入排序
- 希爾排序
- 選擇排序
- 冒泡排序
- 計數(shù)排序
- 基數(shù)排序
- 歸并排序
- 快速排序
- 雙向掃描的快速排序
- 堆排序
- 替換字符串中的空格
- 輸入一個字符串,打印出該字符串的所有排列
- 第一個只出現(xiàn)一次的字符
- 翻轉(zhuǎn)句子
- 計算字符串之間的最短距離
- 查找字符串中的最長重復(fù)子串
- 求長度為
N
的字符串的最長回文子串 - 將字符串中的
*
移到前部,并且不改變非*
的順序 - 不開辟用于交換的空間,完成字符串的逆序
C++
- 最短摘要生成
- 最長公共子序列
- 二維數(shù)組的整數(shù)查找
- 旋轉(zhuǎn)數(shù)組中的最小數(shù)字(旋轉(zhuǎn)數(shù)組中的最大數(shù)字)
- 調(diào)整數(shù)組使奇數(shù)位于偶數(shù)之前
- 找出數(shù)組中出現(xiàn)次數(shù)超過一半的數(shù)字
- 找到最小的
k
個數(shù) - 連續(xù)子數(shù)組的最大和
- 連續(xù)子數(shù)組的最大和(二維)
- 求數(shù)組當(dāng)中的逆序?qū)?/li>
- 在遞增排序的數(shù)組中,查找指定數(shù)字出現(xiàn)的個數(shù)
- 查找數(shù)組中只出現(xiàn)一次的兩個數(shù)字
- 在遞增排序的數(shù)組中,查找和為
s
的兩個數(shù) - 輸入一個正數(shù)
s
,打印出所有和為s
的連續(xù)正數(shù)序列 - 數(shù)組當(dāng)中的最大最小值
- 求數(shù)組當(dāng)中的最長遞增子序列(求數(shù)組當(dāng)中的最長遞減子序列)
- 區(qū)間重合判斷
- 一個整數(shù)數(shù)組,長度為
n
,將其分為m
份,使各份的和相等,求m
的最大值
- 普通二分查找
- 查找關(guān)鍵字第一次出現(xiàn)的位置
- 查找關(guān)鍵字最后一次出現(xiàn)的位置
- 查找小于關(guān)鍵字的最大數(shù)字出現(xiàn)的位置
- 查找大于關(guān)鍵字的最小數(shù)字出現(xiàn)的位置
- 新建鏈表
- 反轉(zhuǎn)鏈表(遞歸和非遞歸實現(xiàn))
- 獲得鏈表倒數(shù)第
k
個結(jié)點 - 獲得鏈表的中間結(jié)點
- 刪除鏈表結(jié)點
- 交換鏈表結(jié)點
- 建立二叉查找樹
- 刪除二叉查找樹中指定元素
- 非遞歸遍歷二叉查找樹(先序遍歷、中序遍歷、后序遍歷)
面試算法知識梳理(11) - 二叉樹相關(guān)算法第一部分
- 遞歸遍歷二叉樹(先序遍歷、中序遍歷、后序遍歷)
- 分層打印二叉樹
- 打印二叉樹的第
n
層 - 統(tǒng)計二叉樹葉結(jié)點的個數(shù)
- 統(tǒng)計二叉樹的高度
- 獲得二叉樹的鏡像
- 判斷元素是否存在于二叉樹中
- 打印二叉樹中和為
s
的路徑 - 獲得二叉樹的最大距離
- 判斷二叉樹是否是平衡樹
- 將二叉樹轉(zhuǎn)換成為鏈表
- 判斷數(shù)組是否為二叉樹的后序遍歷
- 判斷某樹是否是另一棵樹的子樹
- 根據(jù)前序和中序序列重建二叉樹
一、概述
- 將二叉樹轉(zhuǎn)換成為鏈表
- 判斷數(shù)組是否為二叉樹的后序遍歷
- 判斷某樹是否是另一棵樹的子樹
- 根據(jù)前序和中序序列重建二叉樹
二、代碼實現(xiàn)
2.1 將二叉樹轉(zhuǎn)換成為鏈表
解決思路
采用后序遍歷的思路,先將左右子樹轉(zhuǎn)換成鏈表,再將左右子樹的鏈表通過中間結(jié)點連接起來。
代碼實現(xiàn)
public class Untitled {
static class Tree {
int size;
Node root;
}
static class Node {
Node parent;
Node left;
Node right;
int value;
}
static class ListValue {
Node header;
Node tail;
}
static void insertNode(Tree tree, int value) {
if (tree == null) {
return;
}
Node tNode = tree.root;
//待插入結(jié)點的父結(jié)點,如果遍歷完為空,說明此時是一個空樹。
Node pNode = null;
//新的結(jié)點。
Node nNode = new Node();
nNode.value = value;
while (tNode != null) {
pNode = tNode;
if (tNode.value > value) {
tNode = tNode.left;
} else {
tNode = tNode.right;
}
}
nNode.parent = pNode;
if (pNode == null) {
tree.root = nNode;
} else if (pNode.value > value) {
pNode.left = nNode;
} else {
pNode.right = nNode;
}
tree.size++;
}
static Tree createBinTree(int p[], int len) {
Tree tree = new Tree();
for (int i = 0; i < len; i++) {
int value = p[i];
insertNode(tree, value);
}
return tree;
}
//采用后序遍歷的方式轉(zhuǎn)換成鏈表。
static ListValue treeToList(Node p) {
if (p == null) {
return null;
}
ListValue value = new ListValue();
value.header = p;
value.tail = p;
//左子樹部分的鏈表。
ListValue leftNode = treeToList(p.left);
//右子樹部分的鏈表。
ListValue rightNode = treeToList(p.right);
//左子樹部分的尾結(jié)點作為p的前驅(qū)節(jié)點,右子樹部分的頭結(jié)點作為p的后繼結(jié)點。
if (leftNode != null) {
leftNode.tail.right = p;
p.left = leftNode.tail;
value.header = leftNode.header;
}
if (rightNode != null) {
rightNode.header.left = p;
p.right = rightNode.header;
value.tail = rightNode.tail;
}
return value;
}
static void printTreeList(ListValue value) {
Node node = value.tail;
while (node != null && node.left != null) {
System.out.println(node.value);
node = node.left;
}
}
public static void main(String[] args) {
int p[] = {3, 5, 6, 1, 2, 4, -1, -3};
Tree tree = createBinTree(p, p.length);
ListValue value = treeToList(tree.root);
printTreeList(value);
}
}
運行結(jié)果
6
5
4
3
2
1
-1
2.2 判斷數(shù)組是否為二叉查找樹的后序遍歷
問題描述
輸入一個整數(shù)數(shù)組,判斷該數(shù)組是不是某二叉查找樹的后序遍歷的結(jié)果,假設(shè)輸入的數(shù)組的任意兩個數(shù)字都互不相同。
解決思路
依據(jù)題目的描述我們可以得到關(guān)鍵信息:
- 這棵樹為二叉查找樹,因此對于任意結(jié)點,它的左子樹小于該結(jié)點,右子樹大于該結(jié)點,并且左右子樹都是二叉查找樹。
- 如果是后序遍歷,那么對于任意子樹,它的根結(jié)點是該部分數(shù)組的最后一個元素。
代碼實現(xiàn)
public class Untitled {
static boolean isPostOrderOfTree(int p[], int startIndex, int endIndex) {
if (startIndex == endIndex) {
return true;
}
int root = p[endIndex];
int middle = startIndex;
//通過根結(jié)點將左右子樹分開。
while (p[middle] < root && middle < endIndex) {
middle++;
}
//驗證右子樹是否都比根結(jié)點要大。
for (int i = middle; i < endIndex; i++) {
if (!(p[i] > root)) {
return false;
}
}
//說明該結(jié)點沒有左子樹。
if (middle == startIndex || middle == endIndex) {
return isPostOrderOfTree(p, startIndex, endIndex - 1);
} else {
//先驗證左邊的數(shù)組是否是后序遍歷。
boolean left = isPostOrderOfTree(p, startIndex, middle - 1);
if (left) {
//再驗證右邊的數(shù)組是否是后序遍歷。
return isPostOrderOfTree(p, middle, endIndex - 1);
} else {
return false;
}
}
}
public static void main(String[] args) {
int p1[] = {5, 7, 6, 9, 11, 10, 8};
System.out.println("p1 是二叉查找樹的后序遍歷=" + isPostOrderOfTree(p1, 0, p1.length - 1));
int p2[] = {7, 10, 8, 9};
System.out.println("p2 是二叉查找樹的后序遍歷=" + isPostOrderOfTree(p2, 0, p2.length - 1));
}
}
運行結(jié)果
>> p1 是二叉查找樹的后序遍歷=true
>> p2 是二叉查找樹的后序遍歷=false
2.3 判斷某樹是否是另一棵樹的子樹
解決思路
先判斷父樹和子樹的根結(jié)點是否相等,如果相等,再比較兩棵樹是否完全相同,如果根結(jié)點不相等,那么再遞歸比較父樹的左子樹和子樹,以及父樹的右子樹和子樹。
代碼實現(xiàn)
public class Untitled {
static class Tree {
int size;
Node root;
}
static class Node {
Node parent;
Node left;
Node right;
int value;
}
static void insertNode(Tree tree, int value) {
if (tree == null) {
return;
}
Node tNode = tree.root;
//待插入結(jié)點的父結(jié)點,如果遍歷完為空,說明此時是一個空樹。
Node pNode = null;
//新的結(jié)點。
Node nNode = new Node();
nNode.value = value;
while (tNode != null) {
pNode = tNode;
if (tNode.value > value) {
tNode = tNode.left;
} else {
tNode = tNode.right;
}
}
nNode.parent = pNode;
if (pNode == null) {
tree.root = nNode;
} else if (pNode.value > value) {
pNode.left = nNode;
} else {
pNode.right = nNode;
}
tree.size++;
}
static Tree createBinTree(int p[], int len) {
Tree tree = new Tree();
for (int i = 0; i < len; i++) {
int value = p[i];
insertNode(tree, value);
}
return tree;
}
static boolean isSubTree(Node node, Node subNode) {
if (node == null || subNode == null) {
return false;
}
boolean result = false;
if (node.value == subNode.value) {
//如果結(jié)點相等,那么比較樹是否相等。
result = isTreeEquals(node, subNode);
}
if (!result && node.left != null) {
//是否是左子樹的子樹。
result = isSubTree(node.left, subNode);
}
if (!result && node.right != null) {
//是否是左子樹的子樹。
result = isSubTree(node.right, subNode);
}
return result;
}
static boolean isTreeEquals(Node node1, Node node2) {
if (node1 == null && node2 == null) {
return true;
}
if (node1 == null) {
return false;
}
if (node2 == null) {
return false;
}
if (node1.value != node2.value) {
return false;
}
return isTreeEquals(node1.left, node2.left) && isTreeEquals(node1.right, node2.right);
}
public static void main(String[] args) {
int p1[] = {3, 5, 6, 1, 2, 4, -1, -3};
Tree tree = createBinTree(p1, p1.length);
int p2[] = {1, 2, -1, -3};
Tree subTree = createBinTree(p2, p2.length);
int p3[] = {1, 2, -1, -3, -5};
Tree subTree2 = createBinTree(p3, p3.length);
System.out.println("p2 是 p1 的子樹=" + isSubTree(tree.root, subTree.root));
System.out.println("p3 是 p1 的子樹=" + isSubTree(tree.root, subTree2.root));
}
}
運行結(jié)果
>> p2 是 p1 的子樹=true
>> p3 是 p1 的子樹=false
2.4 根據(jù)先序遍歷和中序遍歷重建二叉樹
解決思路
根據(jù)先序遍歷的特點,其第一個元素為樹的根結(jié)點,然后在中序遍歷的結(jié)點中找到該根結(jié)點,分為左右兩個子樹部分,遞歸進行求解。
代碼實現(xiàn)
public class Untitled {
static class Tree {
int size;
Node root;
}
static class Node {
Node parent;
Node left;
Node right;
int value;
}
static Node createTree(int preOrder[], int preStart, int preEnd, int inOrder[], int inStart, int inEnd) {
Node node = new Node();
int root = preOrder[preStart];
node.value = root;
//如果只有一個元素,那么直接返回即可。
if (preStart == preEnd) {
return node;
}
int rootIndex = inStart;
while (rootIndex < inEnd && inOrder[rootIndex] != root) {
rootIndex++;
}
int leftPreOrderEnd = preStart + (rootIndex - inStart);
if (rootIndex != inStart) {
node.left = createTree(preOrder, preStart + 1, leftPreOrderEnd, inOrder, inStart, rootIndex - 1);
}
if (rootIndex != inEnd) {
node.right = createTree(preOrder, leftPreOrderEnd + 1, preEnd, inOrder, rootIndex + 1, inEnd);
}
return node;
}
static void printPreOrder(Node node) {
if (node == null) {
return;
}
System.out.println(node.value);
printPreOrder(node.left);
printPreOrder(node.right);
}
static void printInOrder(Node node) {
if (node == null) {
return;
}
printInOrder(node.left);
System.out.println(node.value);
printInOrder(node.right);
}
public static void main(String[] args) {
int p1[] = {1, 2, 4, 7, 3, 5, 6, 8};
int p2[] = {4, 7, 2, 1, 5, 3, 8, 6};
Node root = createTree(p1, 0, p1.length - 1, p2, 0, p2.length - 1);
System.out.println("- 先序遍歷 -");
printPreOrder(root);
System.out.println("- 中序遍歷 -");
printInOrder(root);
}
}
運行結(jié)果
- 先序遍歷 -
1
2
4
7
3
5
6
8
- 中序遍歷 -
4
7
2
1
5
3
8
6
更多文章,歡迎訪問我的 Android 知識梳理系列:
- Android 知識梳理目錄:http://www.lxweimin.com/p/fd82d18994ce
- Android 面試文檔分享:http://www.lxweimin.com/p/8456fe6b27c4