# LeetCode深度优先算法之树(树转换)

### 105. 从前序与中序遍历序列构造二叉树

#### 题目描述

``````    3
/ \
9  20
/  \
15   7
``````

#### 思路

• 在前序遍历序列中找到树的根节点
• 在中序序列中找到这个根节点
• 然后递归构建子树

• 递归的结束条件
• 递归的顺序结构

``if (instart == inEnd || preStart == preEnd) return;``

``````ensure the root node;
recursion left;
recursion right;``````

#### 代码实现

``````class Solution {

private Map<Integer, Integer> map = new HashMap<>();

public TreeNode buildTree(int[] preorder, int[] inorder) {

int index = 0;
for (int cur : inorder) {
map.put(cur, index++);
}

return build(preorder, inorder, 0, preorder.length, 0, inorder.length);
}

TreeNode build(int[] pre, int[] in, int pStart, int pEnd, int inStart, int inEnd) {
if (pStart == pEnd) return null;

int rootVal = pre[pStart];
int rootIndex = map.get(rootVal);

TreeNode root = new TreeNode(rootVal);
int leftNum = rootIndex - inStart;

root.left = build(pre, in, pStart + 1, pStart + 1 + leftNum, inStart, rootIndex);
root.right = build(pre, in, pStart + 1 + leftNum, pEnd, rootIndex + 1, inEnd);

return root;

}
}
``````

### 106.从中序与后序遍历序列构造二叉树

#### 题目描述

``````根据一棵树的中序遍历与后序遍历构造二叉树。

3
/ \
9  20
/  \
15   7
``````

#### 代码

``````class Solution {

Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {

int index = 0;
for (int cur : inorder) {
map.put(cur, index++);
}

return build(inorder, postorder, 0, inorder.length, 0, postorder.length);
}

TreeNode build(int[] in, int[] post,int iStart, int iEnd, int pStart, int pEnd) {
if (iStart == iEnd || pStart == pEnd) return null;

int rootVal = post[pEnd - 1];
TreeNode root = new TreeNode(rootVal);

int rootIndex = map.get(rootVal);
int leftNum = rootIndex - iStart;

root.left = build(in, post, iStart, rootIndex, pStart, pStart + leftNum);
root.right = build(in, post, rootIndex + 1, iEnd, pStart + leftNum, pEnd - 1);

return root;
}
}
``````

### 108. 将有序数组转换为二叉搜索树

#### 题目描述

``````      0
/ \
-3   9
/   /
-10  5``````

#### 代码

``````class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length == 0) return null;

return build(nums, 0, nums.length - 1);
}

TreeNode build(int[] nums, int left, int right) {
if (left > right) return null;

int mid = (right - left) / 2 + left;
TreeNode root = new TreeNode(nums[mid]);
root.left = build(nums, left, mid - 1);
root.right = build(nums, mid + 1, right);

return root;
}
}``````

### 109.有序链表转换二叉搜索树

#### 题目描述

``````
0
/ \
-3   9
/   /
-10  5
``````

#### 思路

``````slow = node;
fast = node;
while (fast.next != boarder && != fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}

return slow;``````

#### 代码

``````class Solution {
if (head == null) return null;

}

TreeNode build(ListNode left, ListNode right) {
if (left == right) return null;
ListNode mid = getMid(left, right);

TreeNode root = new TreeNode(mid.val);
root.left = build(left, mid);
root.right = build(mid.next, right);

return root;
}

ListNode getMid(ListNode left, ListNode right){
ListNode slow = left;
ListNode fast = left;

while (fast.next != right && fast.next.next != right) {
slow = slow.next;
fast = fast.next.next;
}

return slow;
}``````

### 114.二叉树展开为链表

#### 题目描述

``````    1
/ \
2   5
/ \   \
3   4   6

1
\
2
\
3
\
4
\
5
\
6``````

#### 代码

``````class Solution {

List<TreeNode> list = new ArrayList<>();
public void flatten(TreeNode root) {
if (root == null) return ;

pre(root);
TreeNode pre = null;
TreeNode cur = null;
for (int i = 1; i < list.size(); i++) {
pre = list.get(i - 1);
cur = list.get(i);

pre.left = null;
pre.right = cur;
}
}

void pre(TreeNode root) {
if (root != null) {

pre(root.left);
pre(root.right);
}
}
}
``````
0 声望
0 粉丝
0 条评论