problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::create_binary_tree_from_descriptions::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static TreeNodecreateBinaryTree (vector< vector< int > > &descriptions)
 

详细描述

在文件 leetcode.h1544 行定义.

成员函数说明

◆ createBinaryTree()

TreeNode * leetcode::create_binary_tree_from_descriptions::Solution::createBinaryTree ( vector< vector< int > > &  descriptions)
static

在文件 leetcode.cpp3965 行定义.

3965 {
3966 unordered_map<int, TreeNode *> um;
3967 unordered_set<int> nodes;
3968 unordered_set<int> childs;
3969 for(auto description: descriptions) {
3970 nodes.insert(description[0]);
3971 nodes.insert(description[1]);
3972 childs.insert(description[1]);
3973 }
3974 for(auto node: nodes) {
3975 um[node] = new TreeNode();
3976 }
3977 for(auto description: descriptions) {
3978 if(description[2] == 1) {
3979 //childi 是 parenti 的左子节点
3980 um[description[0]]->left = um[description[1]];
3981 } else {
3982 //childi 是 parenti 的右子节点。
3983 um[description[0]]->right = um[description[1]];
3984 }
3985 }
3986 for(auto node: nodes) {
3987 if(!childs.contains(node)) {
3988 return um[node];
3989 }
3990 }
3991 return nullptr;
3992 }

该类的文档由以下文件生成: