Given a binary tree, return all paths from the root to leaves.
For example, given the tree:
1
/ \
2 3
/ \
4 5
Return [[1, 2], [1, 3, 4], [1, 3, 5]]
.
Example:
Input: tree[]=[1,2,3,null,null,4,5]
Output: [[1,2,],[1,3,4,],[1,3,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;}};vector<vector<int>> res;void dfs(TreeNode *root, vector<int> path){if (root == NULL)return;path.push_back(root->data);//If the node is leaf nodeif (root->left == NULL && root->right == NULL)res.push_back(path);//If the node is not a leaf nodeelse{//call for left subtreedfs(root->left, path);//call for right subtreedfs(root->right, path);}}vector<string> binaryTreePaths(TreeNode *root){res.clear();vector<int> path;vector<string> ans;dfs(root, path);for (int i = 0; i < res.size(); i++){string str = "";for (int j = 0; j < res[i].size(); j++){str += to_string(res[i][j]);if (j != res[i].size() - 1)str += ",";}ans.push_back(str);}return ans;}int main(){TreeNode *root = new TreeNode(1);root->left = new TreeNode(2);root->right = new TreeNode(3);root->right->left = new TreeNode(4);root->right->right = new TreeNode(5);vector<string> paths = binaryTreePaths(root);cout << "[";for (int i = 0; i < paths.size() - 1; i++){cout << "[";cout << paths[i] << ",";cout << "],";}cout << "[";cout << paths[paths.size() - 1];cout << "]";cout << "]";return 0;}
No comments:
Post a Comment