1156. Binary tree 二叉树找根方法

#include "iostream"

#include "memory.h"

using namespace std;

/*

找根方法:

每个节点输入次数:根节点在输入自己时输入一次,

非根节点在输入父节点时输入一次,在输入自己时又输入

一次,所以输入了两次。所以可以通过计数器,计算每个节点

被输入的次数来找出根节点。

*/

const int MAX = 1002;

struct Node{

int id;

char val;

int left, right;

Node(char c, int i = 0){

id = i;

val = c;

left = 0;

right = 0;

}

Node(){

left = 0;

right = 0;

}

};

void preOrder(int root, Node *node){

if (root != 0){

cout << node[root].val;

preOrder(node[root].left, node);

preOrder(node[root].right, node);

}

}

int main(){

int n;

while (cin >> n){

int isroot[MAX];

Node node[MAX];

memset(isroot, 0, sizeof(isroot));

int id, left, right;

char value;

for (int i = 0; i < n; i++){

cin >> id >> value >> left >> right;

node[id].id = id;

node[id].val = value;

node[id].left = left;

node[id].right = right;

isroot[id]++;

isroot[left]++;

isroot[right]++;

}

int root;

for (int i = 0; i < MAX; i++){

if (isroot[i] % 2 == 1){ //输入次数为次数的节点为根

root = i;

}

}

preOrder(root, node);

cout << endl;

}

return 0;

}

时间: 2024-12-29 18:21:42

1156. Binary tree 二叉树找根方法的相关文章

Sicily 1156. Binary tree

题目地址:1156. Binary tree 思路: 如何愉快地前序输出呢,要在存储数据的时候根据位置来存放数据! 一开始被自己蠢哭,一直以为第一个输入的就是根结点(例子的祸害呀啊啊啊!!!!),结果证明不是的,所以呢,我们要找出根结点,那么怎么找根结点呢,我用了一个向量来存储下标值,遍历向量值,把节点的左右值作为下标的那个数据设为非根结点,然后剩下的那个就是根节点了. 具体代码如下: 1 #include <iostream> 2 #include <vector> 3 usin

UVALive 6577 Binary Tree 二叉树的LRU串

今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一个向下无限延伸的二叉树.然后输入起点(通过只含LRU的字符串S,从根结点开始执行).LRU分别表示往左儿子走,往右儿子走,往爹娘处走(根结点的爹娘是自己,估计他是石头里蹦出来的). 然后输入一个可选步骤串T.可以选择T中的子序,从起点开始走.然后问可以走到多少个不同的结点. 比赛的时候不会做啊╮(╯

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

lintcode 容易题:Maximum Depth of Binary Tree 二叉树的最大深度

题目: 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / 4 5 这个二叉树的最大深度为3. 解题: 递归方式求树的深度,记住考研时候考过这一题 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeN

104 Maximum Depth of Binary Tree 二叉树的最大深度

给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回最大深度为 3 .详见:https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ 方法一:非递归 /** * Definition for a binary tree node. * st

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 /** 2 *

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. 求二叉树的最大深度问题用到深度优先搜索DFS,递归的完美应用,跟求二叉树的最小深度问题原理相同.代码如下: /** * Definition for binary tree * s

[LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w

671. Second Minimum Node In a Binary Tree 二叉树中第二小节点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G