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 ...
随机推荐
- jquery判断滚动条到底
$(document).scroll(function(){ var dHeight = $(document).height(); var wHeight = $(window).height(); ...
- [JavaScript] JavaScript作用域深度解析
JavaScript作用域 JavaScript中的函数运行在它们被定义的作用域里,而不是它们被执行的作用域里. -- JS权威指南 在JS里,一切皆对象,函数也是. 一.有什么用 什么时候会用到它? ...
- redis 异常排查
异常排查 redis-server redis.windows.conf D:\redis-2.8.17>redis-server.exe redis.windows.conf[4692] 27 ...
- 谈B2B电商平台与大数据
数据为王,服务为本——谈B2B电商平台与大数据 2013-06-27 11:10:41 作者:B2B行业资讯 标签: 大数据 ...
- C#的checked和unchecked
C#的 checked关键字用于对整型算术运算和转换显式启用溢出检查. 简单点说,我们在进行数值计算时,运算结果可能会超出该类型能表达的数值范围,因而结果溢出.而这个溢出如果是含有变量的表达式的话,编 ...
- 教你正确地利用Netty建立连接池
一.问题描述 Netty是最近非常流行的高性能异步通讯框架,相对于Java原生的NIO接口,Netty封装后的异步通讯机制要简单很多. 但是小K最近发现并不是所有开发人员在使用的过程中都了解其内部实现 ...
- java中的浮点(float)运算
一. 关于浮点运算,需要说明的几点: 1. 在java中,进行浮点运算并不会处理例外情况,所以,即使除数为0,也不会有例外被抛出; 2. 当运算结果是溢出(Infinity)时,结果为Infin ...
- Create a simple REST web service with Python--转载
今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...
- JDBC编程之优化
1.创建 dbconfig.properties driver=com.mysql.jdbc.Driver dburl=jdbc\:mysql\://localhost\:3306/mytest us ...
- [Qt] qtcreator 中打开console
(1) qtcreator-->左侧Projects-->Run-->中间的checkbox (Run in terminal)打上勾 (2) 在项目的.pro文件中加上 " ...