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 ...
随机推荐
- Apache的配置
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) (1) 基本配置: ServerRoot "/mnt/s ...
- 解压Taobao手机客户端发现的东西
今天解压了Taobao手机客户端发现了几个.so文件, 其中有两个挺感兴趣的,查了一下,以后去研究下. libBSPatch.so 是用于支持增量更新功能的库文件. libwebp.so 好像是We ...
- 离线破win2003域账号密码(更新域数据库不在默认路径的情况)
国内网站讲了许多方法,很多也讲得不清楚.我把我尝试成功的经验跟大家分享. 方法是按照tim的文章操作,链接如下: http://pauldotcom.com/2011/11/safely-dumpin ...
- ASP.NET Web.Config 读资料 (学习笔记)
refer : http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 上面这篇写很好了. 在做项目时,我们经常会遇到一些资料,我 ...
- MFC关于VS内存释放的定位
全部在App中完成 1.在 App.h 头文件声明 #ifdef _DEBUGprotected: CMemoryState m_msOld, m_msNew, m_msDiff;#endi ...
- 创建Windows服务(Windows Services)N种方式总结
最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshel ...
- 通过JS触发TextBox的ontextchanged事件,并获取TextBox所在GridView的那一行
protected void txtInsNum_TextChanged(object sender, EventArgs e) { TextBox t = (TextBox)sender; Grid ...
- 浅谈C#抽象类和C#接口
原文地址:http://www.cnblogs.com/zhxhdean/archive/2011/04/21/2023353.html 一.C#抽象类: C#抽象类是特殊的类,只是不能被实例化:除此 ...
- 再看C
1. clrscr(void) 清屏 clear screen;gotoxy(x,y); 移动光标至指定位置;
- hadoop2.2.0 MapReduce的序列化
package com.my.hadoop.mapreduce.dataformat; import java.io.DataInput;import java.io.DataOutput;impor ...