XM6. 树的高度
描述
现在有一棵合法的树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度输入描述
输入的第一行表示节点的个数n(2 ≤ n ≤ 1000,节点的编号为0到n-1)组成, 下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号输出描述
输出树的高度,为一个整数示例1
输入:
5 0 1 0 2 1 3 1 4
输出:
3
C 解法, 执行用时: 1ms, 内存消耗: 348KB, 提交时间: 2021-05-11
#include <stdio.h> #include <stdlib.h> int main(void) { int num, p, c, h, tmp; for( ; scanf("%d", &num)!=EOF; ){ int tree_structure[1008] = {0}; tree_structure[0] = h = 1; for(int i=1; i<num; i++){ scanf("%d%d", &p, &c); if( !(tmp=tree_structure[p]) ){ printf("Another Tree Root Node!!\n"); continue; } tree_structure[c] = tmp+1; if(tmp == h) h++; } printf("%d\n", h); } return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 372KB, 提交时间: 2017-10-13
#include<iostream> using namespace std; class Node{ int data; Node *leftChild; Node *rightChild; public: Node(){leftChild = rightChild = NULL;} void setLeft(int d){ leftChild = new Node(); leftChild->data = d; } void setData(int d){ data = d; } int getData(){ return data; } Node *getLeftChild(){ return leftChild; } Node *getRightChild(){ return rightChild; } void setRight(int d){ rightChild = new Node(); rightChild->data = d; } inline bool leftEmpty(){ return leftChild == NULL; } inline bool rightEmpty(){ return rightChild == NULL; } }; class Tree{ Node *root; void del(Node *r){ if(r == NULL)return; del(root->getLeftChild()); del(root->getRightChild()); delete r; } public: Tree(){root = NULL;} void connect(const int &p,const int &c){ if(root == NULL){ root = new Node(); root -> setData(p); root -> setLeft(c); }else{ preOrder(root,p,c); } } int getHeight(){ return height(root); } int height(Node* r){ if(r == NULL)return 0; if(r->leftEmpty() && r->rightEmpty())return 1; int left,right; left = height(r->getLeftChild()); right = height(r->getRightChild()); return (left>right?left:right)+1; } bool preOrder(Node *r,const int &p,const int &c){ if(r == NULL)return false; if(r->getData() == p){ if(r->leftEmpty()){ r->setLeft(c); }else if(r->rightEmpty()){ r->setRight(c); }else return false; return true; } if(preOrder(r->getLeftChild(),p,c)) return true; if(preOrder(r->getRightChild(),p,c)) return true; return false; } ~Tree(){ if(root != NULL) delete(root); } }; int main(){ int n; int num_parent,num_child; Tree tree; cin>>n; for(int i = 0;i < n-1;++i){ cin>>num_parent>>num_child; tree.connect(num_parent,num_child); } cout<<tree.getHeight()<<endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-11-04
#include <iostream> #include <vector> using namespace std; int main() { int n,H = 1; int i = 0; int f,c, h; vector<int> nodes(1000, 0); //有效节点的高度 nodes[0] = 1; // 题目说了至少有一个节点,高度只是是1 vector<int> childnum(1000, 0); //记录节点的孩子数量 cin >> n; while(--n){ cin >> f >> c; //父节点不存在 或者 父节点已有两个子节点 就跳过 if(nodes[f]==0 || childnum[f] == 2) continue; childnum[f] += 1; h = nodes[f] + 1; nodes[c] = h; if(h > H) H = h; } cout << H; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-10-10
#include<iostream> #include<vector> #include<queue> using namespace std; int main(){ int n; cin>>n; vector<vector<int>> ad(n,{-1}); queue<int> w; int depth=1; int temp1,temp2; for(int i=0;i<n-1;i++) { cin>>temp1>>temp2; if(ad[temp1].size()<3) ad[temp1].push_back(temp2); } w.push(0); w.push(-1); while(w.size()!=0) { temp1=w.front(); w.pop(); if(temp1!=-1) { for(int j=1;j<ad[temp1].size();j++) w.push(ad[temp1][j]); } else { if(w.size()!=0) {w.push(-1);depth++;} else {cout<<depth<<endl;return 0;} } } }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-30
#include<iostream> #include<vector> using namespace std; vector<int> bt[1000]; int deepFind(int index) { int len=1; if(0==bt[index].size()) return 1; for(int i=0;(i<bt[index].size())&&(i<2);i++) len=max(len,deepFind(bt[index][i])); return len+1; } int main(int argc,char* argv[]) { int n,k1,k2; cin>>n; while(n) { cin>>k1>>k2; bt[k1].push_back(k2); n--; } cout<<deepFind(0)<<endl; }