Print the nodes in a binary tree level-wise. For example, the following should print 1, 2, 3, 4, 5.
1
/ \
2 3
/ \
4 5
Example:
Input: tree = [1,2,3,null,null,4,5]
Output: [1, 2, 3, 4, 5]
Approach
C++
#include <bits/stdc++.h>using namespace std;//struct for treenodestruct TreeNode{int data;TreeNode *left;TreeNode *right;TreeNode(int data){this->data = data;this->left = NULL;this->right = NULL;}};//Function to find the level order//traversal of the treevector<int> levelOrderTraversal(TreeNode *root){vector<int> res;if (root == NULL)return res;queue<TreeNode *> q;q.push(root);//Run loop till queue is not emptywhile (!q.empty()){int len = q.size();vector<int> vec;while (len--){root = q.front();q.pop();res.push_back(root->data);if (root->left)q.push(root->left);if (root->right)q.push(root->right);}}return res;}int main(){TreeNode *tree = new TreeNode(1);tree->left = new TreeNode(2);tree->right = new TreeNode(3);tree->right->left = new TreeNode(4);tree->right->right = new TreeNode(5);vector<int> res = levelOrderTraversal(tree);int n = res.size();cout << "[";for (int i = 0; i < n; i++){cout << res[i];if (i != res.size() - 1)cout << ", ";}cout << "]";return 0;}//Time Complexity: O(n)
No comments:
Post a Comment