diff --git a/2096. Step-By-Step Directions From a Binary Tree Node to Another b/2096. Step-By-Step Directions From a Binary Tree Node to Another new file mode 100644 index 0000000..045dac2 --- /dev/null +++ b/2096. Step-By-Step Directions From a Binary Tree Node to Another @@ -0,0 +1,95 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void f(TreeNode *root,bool &get,string &str,int st){ + if(get){ + str.pop_back(); + return; + } + if(!root){ + str.pop_back(); + return; + } + if(root->val == st){ + get = true; + return; + } + str+='L'; + f(root->left,get,str,st); + if(get) return; + str+='R'; + f(root->right,get,str,st); + if(get) return; + str+='U'; + str.pop_back(); + str.pop_back(); + } + string getDirections(TreeNode* root, int startValue, int destValue) { + bool get = false; + string ststr = "",endstr="",ans=""; + f(root,get,ststr,startValue); + get = false; + f(root,get,endstr,destValue); + if(ststr==""){ + return endstr; + } + if(endstr==""){ + for(int i=0;i