【每天40分钟,我们一起用50天刷完 (剑指Offer)】第三天

专注 效率 记忆
预习 笔记 复习 做题

本题出自 acwing网站
这个系列是免费的
打卡即刻退回费用。

第三天【剑指Offer例题代码 系列】

    • 6. 重建二叉树
        • 根据前序遍历和中序遍历 得到树
      • 补充题:树的遍历
    • 7. 二叉树的下一个节点

6. 重建二叉树

原题链接

根据前序遍历和中序遍历 得到树

过程如下:

  1. 首先根据前序遍历找到 根节点
  2. 找到中序遍历中,该根节点的位置
  3. 中序中 位于 根节点左边的就是 左子树,右边的就是右子树
  4. 由于我们需要在中序遍历中找到根节点的位置,那么每次都需要遍历中序遍历,不如直接用unordered_map存储数值和位置
  5. 便于写代码,我们可以每次把mp[根节点] 的位置 用变量表示出来

本题的代码不需要死记硬背

就需要明白

由前序确定根节点
由中序确定左右子树的个数
由左右子树的个数确定下一个根节点的位置

根据这三点去写代码即可

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:unordered_map<int,int> pos;TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {int n = preorder.size();for (int i = 0; i < n; i ++ )pos[inorder[i]] = i;return dfs(preorder, inorder, 0, n - 1, 0, n - 1);}TreeNode* dfs(vector<int>&pre, vector<int>&in, int pl, int pr, int il, int ir){if (pl > pr) return NULL;int k = pos[pre[pl]] - il;TreeNode* root = new TreeNode(pre[pl]);root->left = dfs(pre, in, pl + 1, pl + k, il, il + k - 1);root->right = dfs(pre, in, pl + k + 1, pr, il + k + 1, ir);return root;}
};

补充题:树的遍历




#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include<memory>
using namespace std;const  int N = 35;
int n;
int inorder[N], postorder[N];
unordered_map<int, int > leftChile, rightChile;//哈希表保存树,leftChile[i] = j: i 的左儿子是j,rightChilet同理
unordered_map<int, int > h;//保存中序遍历中各节点的位置int dfs(int postorder[], int inorder[], int l1, int r1, int l2, int r2)//构造二叉树
{   if (l1 > r1) return 0;//没有节点,返回0int root = postorder[r1];//根结点为后续遍历的最后一个节点int k = h[root];//找到根节点在序遍历中的位置leftChile[root] = dfs(postorder, inorder, l1, k - 1 - l2 + l1, l2, k - 1);//构造左儿子rightChile[root] = dfs(postorder, inorder,r1-1 - (r2 - (k +1)) , r1 -1, k + 1, r2);//构造右儿子return root;
}int main()
{cin >> n;//输入for (int i = 0; i < n; i++)cin >> postorder[i];for (int i = 0; i < n; i++){cin >> inorder[i];h[inorder[i]] = i;//保存中序遍历中各个节点的位置}int root = dfs(postorder, inorder, 0, n - 1, 0, n - 1);//构造二叉树//数组模拟队列int q[N], hh = 0, tt = -1;//按层次遍历if (root)//非0 表示有节点q[++tt] = root;while (hh <= tt){int t = q[hh++];if (leftChile[t]) q[++tt] = leftChile[t];//非0 为节点,入队列if (rightChile[t]) q[++tt] = rightChile[t];//非0 为节点,入队列}for (int i = 0; i <= tt; i++)//队列中保存的就是按层次遍历的结果cout << q[i] << " ";return 0;
}

7. 二叉树的下一个节点

原题链接

中序遍历:左根右

本题要分析节点的特点

  1. 如果节点有右子树,那么右子树的最左边的节点就是该节点后序
  2. 如果没有右子树,会有三种可能,在代码中有体现
/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode father;*     TreeNode(int x) { val = x; }* }*/
class Solution {/*** 模拟* 时间复杂度:O(height),height 为二叉树的高度* 空间复杂度:O(1)*/public TreeNode inorderSuccessor(TreeNode p) {TreeNode node = p;// Case 1. 如果该节点有右子树,那么下一个节点就是其右子树中最左边的节点if (node.right != null) {node = node.right;while (node.left != null) {node = node.left;}return node;}if(node.father != null && node.father.left == node)return node.father;if(node.father != null && node.father == null)return null;while(node.father!=null && node.father.right == node){node = node.father;}return node.father;}
}

本文链接:https://my.lmcjl.com/post/1115.html

展开阅读全文

4 评论

留下您的评论.