5.1.10—二叉树的遍历—Balanced Binary Tree
来源:互联网 发布:开淘宝网店详细步骤 编辑:程序博客网 时间:2024/06/15 05:10
描述
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the
two subtrees of every node never differ by more than 1.
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the
two subtrees of every node never differ by more than 1.
#include "BinaryTree.h"#include <stack>#include<queue>#include<vector>#include<algorithm>#include<cmath>using namespace std;//===判断一棵二叉树是否是平衡树----递归版本int HeightTree(BinaryTreeNode *proot){if (!proot) return 0;elsereturn HeightTree(proot->m_pLeft) > HeightTree(proot->m_pRight) ? 1 + HeightTree(proot->m_pLeft) : HeightTree(proot->m_pRight) + 1;}bool IsBalancedTree(BinaryTreeNode *proot){if (!proot)return true;int left = HeightTree(proot->m_pLeft);int right = HeightTree(proot->m_pRight);if (abs(left - right) > 1)return false;return IsBalancedTree(proot->m_pLeft)&&IsBalancedTree(proot->m_pRight);}// ====================测试代码====================// 8// 6 10// 5 7 9 11int main(){//===BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);ConnectTreeNodes(pNode8, pNode6,pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);//===//PrintTree(pNode8);//===bool flag = IsBalancedTree(pNode8);cout << flag << endl;DestroyTree(pNode8);}
阅读全文
0 0
- 5.1.10—二叉树的遍历—Balanced Binary Tree
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- 二叉树遍历应用110. Balanced Binary Tree
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- Balanced Binary Tree 平衡二叉树的检验
- Balanced Binary Tree 平衡二叉树的判断
- LeetCode:Balanced Binary Tree(平衡二叉树的判断)
- Balanced Binary Tree 二叉平衡树
- 【LeetCode】Balanced Binary Tree,平衡二叉树
- [LeetCode] Balanced Binary Tree 平衡二叉树
- Leetcode Balanced Binary Tree 平衡二叉树
- 平衡二叉树---Balanced Binary Tree
- [LeetCode][二叉树]Balanced Binary Tree
- [LeetCode][二叉树]Balanced Binary Tree
- lintcode balanced-binary-tree 平衡二叉树
- Balanced Binary Tree(平衡二叉树)
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- 5.1.2—二叉树的遍历—Binary Tree Inorder Traversal
- ural1143 Electric Path (区间dp)
- 在APP中播放音频文件
- 5.1.9—二叉树的遍历—Symmetric Tree
- HTTPS 服务搭建相关概念
- 卷积神经网络
- 5.1.10—二叉树的遍历—Balanced Binary Tree
- Java内存回收与分配
- 5.1.11—二叉树的遍历—Flatten Binary Tree to Linked List
- ?
- ConcurrentHashMap
- springmvc笔记-7-文件上传下载,拦截器
- hdu 6070 Dirt Ratio(二分+线段树)(2017 Multi-University Training Contest
- 5.1.12—二叉树的遍历—Populating Next Right Pointers in Ea Node II
- 开发环境配置(hibernate4)和简单增删改查