题目地址: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. Number对象

    <script type="text/javascript"> /* Number对象. 创建Number对象的方式: 方式1: var 变量= new Number( ...

  2. office2003万能密钥

    保证有效 OFFICE 2003 :  GWH28-DGCMP-P6RC4-6J4MT-3HFDY

  3. jdk8新特性之lambda expressions

    本文分两部分: 语法简单说明 lambda的使用 注:这两部分内容均以类+注释的方式进行说明,并且内容均来自官方教程(https://docs.oracle.com/javase/tutorial/j ...

  4. Head First --- Python 第一章

    List 1. python -V 查看python当前版本 2. list.pop() 删除列表里的最后一个元素,并返回删除元素的值 3. list.extend(['a','b','c']) 在原 ...

  5. 转:SetWindowText 的用法

    SetWindowText   函数功能:该函数改变指定窗口的标题栏的文本内容(如果窗口有标题栏).如果指定窗口是一个控件,则改变控件的文本内容.然而,SetWindowText函数不改变其他应用程序 ...

  6. 转:AM335X 启动流程

    链接: http://blog.csdn.net/hudaweikevin/article/details/10376585  作者:David_Hu 启动顺序(针对TI OMA3 EVM) linu ...

  7. Vector3.Dot 与Vector3.Cross

    Vector3.Dot(点积) : 点积的计算方式为:  a·b=|a|·|b|cos<a,b>; 其中<a,b>和<b,a> 夹角不分顺序; 物理学中点积用来计算 ...

  8. Storm测试报告

    http://blog.csdn.net/jmppok/article/details/17614431

  9. MFC基本框架

    MFC基本框架 By  小戴 发表于 2006-12-21 15:59:00  MFC 应用程序框架 1. MFC 简介: MFC ( Microsoft Foundation Class )是由 ...

  10. android仿京东、淘宝商品详情页上拉查看详情

    话不多说,直接上干货,基本就是一个scrollview中嵌套两个scrollview或者webview;关键点事处理好子scrollview和父scrollview的触摸.滑动事件已达到想要的效果.大 ...