题目地址:1156. Binary tree

思路:

如何愉快地前序输出呢,要在存储数据的时候根据位置来存放数据!

  一开始被自己蠢哭,一直以为第一个输入的就是根结点(例子的祸害呀啊啊啊!!!!),结果证明不是的,所以呢,我们要找出根结点,那么怎么找根结点呢,我用了一个向量来存储下标值,遍历向量值,把节点的左右值作为下标的那个数据设为非根结点,然后剩下的那个就是根节点了。

具体代码如下:

 #include <iostream>
#include <vector>
using namespace std; struct Store{
char node;
int left;
int right;
bool is_root;
}store[]; void print(int x) {
if (x == ) return;
cout << store[x].node;
print(store[x].left);
print(store[x].right);
}
int main() {
int t;
while (cin >> t) {
int index;
vector<int> store_index;
for (int i = ; i < t; i++) {
cin >> index;
cin >> store[index].node >> store[index].left
>> store[index].right;
store[index].is_root = true;
store_index.push_back(index);
}
for (int i = ; i < t; i++) {
int left = store[store_index[i]].left;
int right = store[store_index[i]].right;
store[left].is_root = false;
store[right].is_root = false;
}
int root_node;
for (int i = ; i < t; i++) {
if (store[store_index[i]].is_root == true) {
root_node = store_index[i];
break;
}
}
print(root_node);
cout << endl;
} return ;
}

Sicily 1156. Binary tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  3. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  4. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  5. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  6. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  7. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. php获取汉字首字母

    php获取汉字首字母,可以用于按字母对数据进行检索排序等. 分享下网上找的代码.亲测有效. function getFirstCharter($str){ if(empty($str)){return ...

  2. 【GoLang】golang 微服务框架 go-kit

    golang-Microservice Go kit - A toolkit for microservices kubernetes go-kit_百度搜索 Peter Bourgon谈使用Go和& ...

  3. 转:fopen()函数

    1.2 文件的输入输出函数 键盘.显示器.打印机.磁盘驱动器等逻辑设备, 其输入输出都可以通过文件管理的方法来完成.而在编程时使用最多的要算是磁盘文件, 因此本节主要以磁盘文件为主, 详细介绍Turb ...

  4. CentOS安装Nginx,并配置nodejs反向代理

    安装介绍 安装位置:/usr/local/nginx nginx安装包下载地址:http://nginx.org/download/nginx-1.7.11.tar.gz 安装依赖软件 安装nginx ...

  5. I2C的读写操作实验

    [实验任务]   利用24C08断电以后存储的数据不消失的特点,可以做一个断电保护装置.首先利用单片机做一个0-99秒的自动计时器.然后随机关断电源,在 通电以后计时器接着断电前的状态继续计时. [实 ...

  6. 点击类名方法名如何连接到相应的Android源代码

    . 找到Android SDK所在路径 “D:\androidDev\android-sdk-windows\” . 在该目录下选择platforms到 “D:\androidDev\android- ...

  7. COJ 0995 WZJ的数据结构(负五)区间操作

    WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...

  8. Android中使用shape制作一个旋转的progressbar

    public class ZNtestResActivity extends Activity { @Override public void onCreate(Bundle savedInstanc ...

  9. android SimpleCursorAdapter的使用

    String[] fields=new String[]{"foodname","price","taste","stuff&qu ...

  10. USACO6.4-Wisconsin Squares:搜索

    Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling past ...