Sicily 1156. Binary tree
题目地址: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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
		
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
 - Leetcode 笔记 110 - Balanced Binary Tree
		
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
 - 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 ...
 - [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
		
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
 - [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, ...
 - [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
		
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
 - [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
		
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
 - [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
		
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
 - [LeetCode] Binary Tree Paths 二叉树路径
		
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
 
随机推荐
- 类似a:hover的伪类的注解
			
a:link { font-size: 14pt; text-decoration: underline; color: blue; } /*设置a对象在未被访问前的样式表属性 .*/ a:hover ...
 - R: for installing package 'RODBC'
			
Today, i try to install a package in R named 'DOBDC', while i meet a message: > install.packages( ...
 - NT头 IMAGE_NT_HEADER
			
typedef struct_IMAGE_NT_HEADERS{ DWORD Signature; // 固定为 0x00004550 “PE00" IMAGE_FILE_HEADER Fi ...
 - 【转】Nginx windows下搭建过程
			
Nginx windows下搭建过程 内容列表: 简要介绍 下载安装 配置测试 一.简要介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器, ...
 - oldboy第三天学习
			
一.上课体验及感受 额第三天了.身心疲惫.上课一脸懵逼,是最标准的诠释.三个星期的疲惫感,更深了.很难,但是还要继续 写好作业.吸收知识. 二.三级列表 menu = { '北京':{ '海淀':{ ...
 - 摇滚吧HTML5!有声前端交互!(Hello, Jsonic!)
			
软件工程师们摆弄1和0编写他们的乌托邦,音乐人门把玩12平均律上的音符构筑他们的伊甸园.最近,我偶然看了<蓝色骨头>这部电影,片中的男主角是位黑客,同时又兼具音乐创作的才华.在现实生活中, ...
 - PHP之mysql笔记
			
1:在php中提供了两个用于连接MySQL数据库服务器的函数. (1)int mysql_connect(hostname[:port][:/path/to/socket],user,pass). ( ...
 - android中如何处理cookie
			
Managing Cookies HttpClient provides cookie management features that can be particularly useful to t ...
 - Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
			
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
 - use "man rsyslogd" for details. To run rsyslog interactively, use "rsyslogd -n"to run it in debug mo
			
zjtest7-frontend:/root# service rsyslog start Starting system logger: usage: rsyslogd [options] use ...