Sicily 1156. Binary tree

题目地址:1156. Binary tree

思路:

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

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

具体代码如下:

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4
 5 struct Store{
 6     char node;
 7     int left;
 8     int right;
 9     bool is_root;
10 }store[1001];
11
12 void print(int x) {
13     if (x == 0) return;
14     cout << store[x].node;
15     print(store[x].left);
16     print(store[x].right);
17 }
18 int main() {
19     int t;
20     while (cin >> t) {
21         int index;
22         vector<int> store_index;
23         for (int i = 0; i < t; i++) {
24             cin >> index;
25             cin >> store[index].node >> store[index].left
26                 >> store[index].right;
27             store[index].is_root = true;
28             store_index.push_back(index);
29         }
30         for (int i = 0; i < t; i++) {
31             int left = store[store_index[i]].left;
32             int right = store[store_index[i]].right;
33             store[left].is_root = false;
34             store[right].is_root = false;
35         }
36         int root_node;
37         for (int i = 0; i < t; i++) {
38             if (store[store_index[i]].is_root == true) {
39                 root_node = store_index[i];
40                 break;
41             }
42         }
43         print(root_node);
44         cout << endl;
45     }
46
47     return 0;
48 }
时间: 2024-11-06 13:02:51

Sicily 1156. Binary tree的相关文章

1156. Binary tree 二叉树找根方法

#include "iostream" #include "memory.h" using namespace std; /* 找根方法: 每个节点输入次数:根节点在输入自己时输入一次, 非根节点在输入父节点时输入一次,在输入自己时又输入 一次,所以输入了两次.所以可以通过计数器,计算每个节点 被输入的次数来找出根节点. */ const int MAX = 1002; struct Node{ int id; char val; int left, right;

sicily 1156 二叉树的遍历 前序遍历,递归,集合操作

1156. Binary tree Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Your task is very simple: Given a binary tree, every node of which contains one upper case character (‘A’ to ‘Z’); you just need to print all characters of this tree in

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[leetcode] 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on