CareerCup-4.1
来源:互联网 发布:禅城数据便民服务中心 编辑:程序博客网 时间:2024/05/29 03:59
Implement a function to check if a tree is balanced For the purposes of this question,
a balanced tree is defined to be a tree such that no two leaf nodes differ in distance
from the root by more than one
a balanced tree is defined to be a tree such that no two leaf nodes differ in distance
from the root by more than one
答案是两次遍历分别求出最大深度与最小深度,我试着一次遍历求出最大和最小深度
#include <iostream>using namespace std;typedef int Data;struct TreeNode{ TreeNode(Data d):data(d){left = right = NULL;}; Data data; TreeNode* left; TreeNode* right;};class Tree{public: Tree():root(NULL){}; TreeNode* root; void insert(Data data, TreeNode* node = NULL) { if(root == NULL) { root = new TreeNode(data); return; } if(node == NULL) node = root; if(data == node->data) { return; } else if(data < node->data) { if(node->left == NULL) node->left = new TreeNode(data); else insert(data, node->left); } else { if(node->right == NULL) node->right = new TreeNode(data); else insert(data, node->right); } }; void inorderPrint(TreeNode* TreeNode) { if(TreeNode == root) cout<<"Print:"<<endl; if(TreeNode != NULL) { inorderPrint(TreeNode->left); cout<<TreeNode->data<<" "; inorderPrint(TreeNode->right); } if(TreeNode == root) cout<<endl<<"End"<<endl; }; void preorderPrint(TreeNode* TreeNode) { if(TreeNode == root) cout<<"Print:"<<endl; if(TreeNode != NULL) { cout<<TreeNode->data<<" "; preorderPrint(TreeNode->left); preorderPrint(TreeNode->right); } if(TreeNode == root) cout<<endl<<"End"<<endl; }; void postorderPrint(TreeNode* node) { if(node == root) cout<<"Print:"<<endl; if(node != NULL) { postorderPrint(node->left); postorderPrint(node->right); cout<<node->data<<" "; } if(node == root) cout<<endl<<"End"<<endl; }; bool isBalanced() { int min=0 ,max=0; calDepth(root, 0, &min, &max); cout<<"Min: "<<min<<endl<<"Max: "<<max<<endl; if(max - min < 2) return true; else return false; }; void calDepth(TreeNode* node, int depth, int* min, int* max) { if(node == NULL) return; depth++; if(node->left == NULL && node->right == NULL) { if(depth < *min || *min <= 0) *min = depth; if(depth > *max || *max <= 0) *max = depth; } calDepth(node->left, depth, min, max); calDepth(node->right, depth, min, max); };};int main(){ Tree tree; tree.insert(5); tree.insert(1); tree.insert(7); tree.insert(4); tree.insert(3); cout<<boolalpha<<tree.isBalanced(); system("pause");};
- CareerCup-4.1
- CareerCup 4.1
- CareerCup 4.1 & 4.3
- CareerCup 1.3
- CareerCup 1.4
- CareerCup 5.7
- CareerCup 14.6
- CareerCup 1.1
- CareerCup 1.2
- CareerCup 1.3
- CareerCup 1.4
- CareerCup 1.5
- CareerCup 1.6
- CareerCup 1.7
- CareerCup 1.8
- CareerCup 2.1
- CareerCup 2.2
- CareerCup 2.3
- ZOJ-1082 Stockbroker Grapevine
- Win7下激活离开模式
- Nvidia Driver activated but not in use
- T-SQL中GO的意思
- Spring Mongodb集成
- CareerCup-4.1
- !!!Obj-C 2.0 -- Chapter 4 Declared Properties
- javascript学习笔记2-正则表达式入门
- ZOJ-1877 Bridge
- SSH 配置
- java程序员菜鸟进阶(九)windows下搭建SVN服务器及配置myeclipse SVN客户端
- WglCreateContext 函数详解
- CareerCup-4.2
- 互芯最新资料大全