当前位置: 首页 > news >正文

石家庄市城乡建设学校网站企业管理培训课程视频

石家庄市城乡建设学校网站,企业管理培训课程视频,多站点网站群的建设与管理,怎样做信息收费网站文章目录二叉树9. 二叉树的最大深度10. 二叉树的最小深度11. 完全二叉树的节点个数12. 平衡二叉树二叉树 9. 二叉树的最大深度 104. 二叉树的最大深度 思路1: 递归找左右子树的最大深度,选择最深的 1(即加上当前层)。 class So…

文章目录

  • 二叉树
    • 9. 二叉树的最大深度
    • 10. 二叉树的最小深度
    • 11. 完全二叉树的节点个数
    • 12. 平衡二叉树

二叉树

9. 二叉树的最大深度

104. 二叉树的最大深度

思路1:
递归找左右子树的最大深度,选择最深的 + 1(即加上当前层)。

class Solution {
public:int maxDepth(TreeNode* root) {if (root == NULL) return 0;return max(maxDepth(root->left), maxDepth(root->right)) + 1;}
};

思路2:
利用队列层序遍历二叉树,找到最大深度

class Solution {
public:int maxDepth(TreeNode* root) {int depth = 0;if (root == NULL) return depth;queue<TreeNode *> que;que.push(root);while (!que.empty()) {int size = que.size();depth++;while (size--) {TreeNode *cur = que.front(); que.pop();if (cur->left) que.push(cur->left);if (cur->right) que.push(cur->right);}}return depth;}
};

10. 二叉树的最小深度

111. 二叉树的最小深度

思路1: 递归法
要注意只有一个子节点的情况不能统计深度,因为它不是叶子节点。深度是指根节点到叶子节点的距离。

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;// 下面的两个判断避免了非叶子节点的情况if (!root->left && root->right) return minDepth(root->right) + 1;if (root->left && !root->right) return minDepth(root->left) + 1;return min(minDepth(root->left), minDepth(root->right)) + 1;}
};

思路2:
利用迭代法层序遍历,遇到第一个叶子节点返回深度。

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;queue<TreeNode *> que;int depth = 0;que.push(root);while (!que.empty()) {depth++;int size = que.size();while (size--) {TreeNode *cur = que.front(); que.pop();if (!cur->left && !cur->right) return depth;if (cur->left) que.push(cur->left);if (cur->right) que.push(cur->right);}}return depth;}
};

11. 完全二叉树的节点个数

222. 完全二叉树的节点个数

思路:
满二叉树的节点数目等于 2depth−12^{depth} - 12depth1 ,利用该条件来避免遍历整个满二叉树,只需要遍历其最左最右两分支的深度即可(O(logn)O(logn)O(logn))。遍历二叉树的递归层数 O(logn)O(logn)O(logn) 。时间复杂度是 O(logn∗logn)O(logn*logn)O(lognlogn)

class Solution {
public:int countNodes(TreeNode* root) {if (root == NULL) return 0;// 利用满二叉树的特性优化时间复杂度TreeNode *curL = root->left;TreeNode *curR = root->right;int depthL = 0;int depthR = 0;while (curL != NULL) {curL = curL->left;depthL++;}while (curR != NULL) {curR = curR->right;depthR++;}if (depthL == depthR) return (2 << depthL) - 1;return countNodes(root->left) + countNodes(root->right) + 1;}
};

12. 平衡二叉树

110. 平衡二叉树

思路1: 递归
后序遍历统计左右子树的高度,比较左右子树高度是否满足条件。

class Solution {
public:bool isBalanced(TreeNode* root) {if (root == NULL) return true;return getHigh(root) == -1 ? false : true;}private:int getHigh(TreeNode *root) {if (root == NULL) return 0;int leftDepth = getHigh(root->left);if (leftDepth == -1) return -1;int rightDepth = getHigh(root->right);if (rightDepth == -1) return -1;if (abs(leftDepth - rightDepth) > 1) return -1; // 如果已经找到不满足底子树,就没必要再遍历了。剪枝return max(leftDepth, rightDepth) + 1;}
};

思路2: 迭代法
用栈模拟递归实现后续遍历统计二叉树深度。
再先序遍历判断左右子树深度是否满足条件。
这里没法剪枝,复杂度并不优秀。

class Solution {
public:bool isBalanced(TreeNode* root) {if (root == NULL) return true;stack<TreeNode *> stk;stk.push(root);while (!stk.empty()) {TreeNode *cur = stk.top(); stk.pop();if (abs(getHigh(cur->left) - getHigh(cur->right)) > 1) return false;if (cur->right) stk.push(cur->right);if (cur->left) stk.push(cur->left);}return true;}private:int getHigh(TreeNode *root) {if (root == NULL) return 0;stack<TreeNode *> stk;int depth = 0, result = 0;stk.push(root);while (!stk.empty()) {TreeNode *cur = stk.top(); stk.pop();if (cur) { // 后序遍历:左右中stk.push(cur); // 中stk.push(NULL);depth++;if (cur->right) stk.push(cur->right); // 右if (cur->left) stk.push(cur->left); // 左} else {cur = stk.top(); stk.pop();depth--; // 回溯}result = result > depth ? result : depth;}return result;}
};
http://www.khdw.cn/news/21183.html

相关文章:

  • 万业网网站建设审核深圳网
  • 高新快速建设网站找哪家免费网络推广软件
  • 泉州关键词排名工具百度seo运营工作内容
  • 茶叶网站建设的优势搜索关键词推荐
  • 有赞商城官网登录seo网络培训学校
  • 网站建设ihuibest国际域名注册网站
  • 地方门户网站开发网络培训心得体会总结
  • 做第三方团购的平台网站百度推广公司哪家比较靠谱
  • 海南网站建设制作今日新闻最新
  • 郑州网站关武汉seo招聘
  • 申请域名之后如何做网站开鲁seo网站
  • wordpress主题插件汉化seo优化百度技术排名教程
  • wordpress把文章标题放进url阿亮seo技术
  • 做外贸 访问国外网站网站模板下载免费
  • 动漫制作专业好不好常州seo排名收费
  • 做网站待遇哪些网站可以免费发广告
  • 聊城网站建设哪家专业营销最好的方法
  • 动态域名可以做网站吗百度推广登录首页官网
  • 自己的网站打不开了线上宣传方式有哪些
  • 网站后台密码如何破解湖南有实力seo优化哪家好
  • 手机网站跟pc网站有什么不同浏览器打开
  • 国家工商网站查询百度一级代理商
  • 长春谁家做网站发表文章的平台有哪些
  • qq客服代码放在wordpress那里seo权重查询
  • 武汉文理学院机电与建筑工程网站百度热搜的含义
  • 领动做的网站怎么样百度竞价推广投放
  • 汕头网站设计制作公司今天的重要新闻
  • 青岛网站建设设计html网页制作模板代码
  • 网博士自助建站系统下载百度搜索大数据
  • 铜陵app网站做招聘百度推广软件