List Leaves (25)

要按从上到下,从左到右的顺序查找叶子节点,可以采用广度优先搜索的办法

#include <iostream>
#include <queue>
using namespace std;
typedef struct{
    int loc;
    int left;
    int right;
}unit;
int n;
unit* a;
int father(int);
void printLeaf(int);
int main()
{
    cin >> n;
    getchar();
    a = (unit*)malloc(n*sizeof(unit));
    for (int i = 0; i < n; i++){
        a[i].loc = i;
        a[i].left = a[i].right = -1;
    }
    for (int i = 0; i < n; i++){
        char left=getchar();
        getchar();
        char right = getchar();
        getchar();
        if (‘0‘ <= left && left <= ‘9‘){
            a[i].left = left - ‘0‘;
        }
        if (‘0‘ <= right&&right <= ‘9‘){
            a[i].right = right - ‘0‘;
        }
    }
    int root = father(0);

    printLeaf(root);

    cin >> n;
}
int father(int son)
{
    for (int i = 0; i < n; i++){
        if (son == a[i].left || son == a[i].right){
            son = i;
            i = 0;
        }
    }
    return son;
}
void printLeaf(int root)
{
    queue<unit> q;
    queue<int> print;

    q.push(a[root]);
    while (q.empty() != true){
        if (q.front().left == -1 && q.front().right == -1){
            print.push(q.front().loc);
            q.pop();
            continue;
        }
        if (q.front().left != -1){
            q.push(a[q.front().left]);
        }
        if (q.front().right != -1){
            q.push(a[q.front().right]);
        }
        q.pop();
    }
    while (print.size() >= 2){
        cout << print.front() << " ";
        print.pop();
    }
    cout << print.front();
}
时间: 2024-11-08 19:18:33

List Leaves (25)的相关文章

pat03-树2. List Leaves (25)

03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case.

03-树2. List Leaves (25) 二叉树的层序遍历

03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each

PAT03-树1. List Leaves (25)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number

PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each inpu

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

Find Leaves of Binary Tree 找二叉树的叶节点

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation:1. Remove the leaves [4, 5, 3] from the tr

pat1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one

LeetCode 404. Sum of Left Leaves (左子叶之和)

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目标签:Tree 这道题目给了我们一个二叉树,让我们找到所有左子叶之和.这里需要另外一个function - sumLeftLeaves 还要一个int

H - The Falling Leaves

Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large woul