1011. Find the external path length
来源:互联网 发布:网络歌手家家 编辑:程序博客网 时间:2024/06/06 12:53
Description
Given the linked representation of binary trees
typedef int T;
struct BinaryNode{
T data; //store data
BinaryNode *left, *right;
BinaryNode(T d, BinaryNode *l=NULL, BinaryNode* r=NULL):data(d), left(l), right(r) {};
};
Implement the following function:
int externalPL(const BinaryNode* root)
// returns the external path lenght of the binary tree root
{
// insert here
}
n外部路径长度
从根到达各个叶节点的路径长度之和
n内部路径长度
从根到达各个分支节点的路径长度之和用层次遍历,一层一层的找叶节点,方法与1003类似,添加标志结点来判断是那一层。
#include <iostream>#include <queue>using namespace std;typedef int T;struct BinaryNode{ T data; //store data BinaryNode *left, *right; BinaryNode(T d, BinaryNode *l=NULL, BinaryNode* r=NULL):data(d), left(l), right(r) {};};int externalPL(const BinaryNode* root)// returns the external path lenght of the binary tree root{ if (root == NULL) { return 0; } int count = 0; int sum = 0; queue<const BinaryNode*> s; s.push(root); const BinaryNode* b = new BinaryNode(-1, NULL, NULL); s.push(b); while (!s.empty()) { const BinaryNode* a = s.front(); s.pop(); if (s.size() == 0) break; //每一层的长度 if (a == b) { count++; s.push(b); } else { if (a->left != NULL) { s.push(a->left); } if (a->right != NULL) { s.push(a->right); } //判断是否为叶节点 if (a->left == NULL && a->right == NULL) { sum += count; } } } return sum; }
0 0
- 1011. Find the external path length
- Gym 100917 F - Find the Length
- Oracle:environment variable "PATH" does not exceed the recommended length
- A* search ,find the shortest path
- Find the Path(栈的运用)
- TCL find the path of ***file
- Find The Shortest Path In Triangle Numbers
- Environment variable: "PATH" - This test checks whether the length of the environment variable "PATH
- The system cannot find the path specified - Rails
- golang The system cannot find the path specified.
- Maximum Path Length Limitation
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Given a string, find the length of the longest substring without repeating characters
- hdu 2290 Find the Path(dij+heap)
- CodeForces 56E-Find the Path(技巧)
- find the install path of some software in linux (ubuntu)
- Bellman-ford algorithm to find the shortest path
- 100.In which situations does the Oracle Data Pump use external tables and not the direct path load w
- myeclipse 切换到Package Exploer的步骤
- poj1724 ROADS (spfa + A*)
- ubuntu linux 下 Phalcon 开发工具安装
- c语言之路--简单的语言格式和注意几点及作业1
- 优秀java学习资料大全
- 1011. Find the external path length
- table的td设置背景图片
- android JBox2D 框架
- SSL连接建立过程分析(2)
- Java中的负数的在计算机中的二进制表示,以及与十进制的相互转换
- Windows Phone App的dump文件实例分析-Stack Overflow
- SSL连接建立过程分析(3)
- 函数指针的两个例子
- 移动搜索之战:360搜索推出独立品牌“好搜”