MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / treeNodeToString

Function treeNodeToString

memo/treenode.cpp:107–129  ·  view source on GitHub ↗

二叉树层序遍历形式输出

Source from the content-addressed store, hash-verified

105
106// 二叉树层序遍历形式输出
107string treeNodeToString(TreeNode* root) {
108 if (root == nullptr) {
109 return "[]";
110 }
111
112 string output = "";
113 queue<TreeNode*> q;
114 q.push(root);
115 while(!q.empty()) {
116 TreeNode* node = q.front();
117 q.pop();
118
119 if (node == nullptr) {
120 output += "null, ";
121 continue;
122 }
123
124 output += to_string(node->val) + ", ";
125 q.push(node->left);
126 q.push(node->right);
127 }
128 return "[" + output.substr(0, output.length() - 2) + "]";
129}
130
131
132

Callers 1

mainFunction · 0.85

Calls 4

pushMethod · 0.45
emptyMethod · 0.45
frontMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected