Skip to content

feat: add solutions to lc problem: No.2096 #3277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,43 @@
*/
class Solution {
public:
unordered_map<int, vector<pair<int, char>>> edges;
unordered_set<int> visited;
string ans;

string getDirections(TreeNode* root, int startValue, int destValue) {
ans = "";
traverse(root);
string t = "";
dfs(startValue, destValue, t);
return ans;
TreeNode* node = lca(root, startValue, destValue);
string pathToStart, pathToDest;
dfs(node, startValue, pathToStart);
dfs(node, destValue, pathToDest);
return string(pathToStart.size(), 'U') + pathToDest;
}

void traverse(TreeNode* root) {
if (!root) return;
if (root->left) {
edges[root->val].push_back({root->left->val, 'L'});
edges[root->left->val].push_back({root->val, 'U'});
private:
TreeNode* lca(TreeNode* node, int p, int q) {
if (node == nullptr || node->val == p || node->val == q) {
return node;
}
if (root->right) {
edges[root->val].push_back({root->right->val, 'R'});
edges[root->right->val].push_back({root->val, 'U'});
TreeNode* left = lca(node->left, p, q);
TreeNode* right = lca(node->right, p, q);
if (left != nullptr && right != nullptr) {
return node;
}
traverse(root->left);
traverse(root->right);
return left != nullptr ? left : right;
}

void dfs(int start, int dest, string& t) {
if (visited.count(start)) return;
if (start == dest) {
if (ans == "" || ans.size() > t.size()) ans = t;
return;
bool dfs(TreeNode* node, int x, string& path) {
if (node == nullptr) {
return false;
}
if (node->val == x) {
return true;
}
path.push_back('L');
if (dfs(node->left, x, path)) {
return true;
}
visited.insert(start);
if (edges.count(start)) {
for (auto& item : edges[start]) {
t += item.second;
dfs(item.first, dest, t);
t.pop_back();
}
path.back() = 'R';
if (dfs(node->right, x, path)) {
return true;
}
path.pop_back();
return false;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func getDirections(root *TreeNode, startValue int, destValue int) string {
var lca func(node *TreeNode, p, q int) *TreeNode
lca = func(node *TreeNode, p, q int) *TreeNode {
if node == nil || node.Val == p || node.Val == q {
return node
}
left := lca(node.Left, p, q)
right := lca(node.Right, p, q)
if left != nil && right != nil {
return node
}
if left != nil {
return left
}
return right
}
var dfs func(node *TreeNode, x int, path *[]byte) bool
dfs = func(node *TreeNode, x int, path *[]byte) bool {
if node == nil {
return false
}
if node.Val == x {
return true
}
*path = append(*path, 'L')
if dfs(node.Left, x, path) {
return true
}
(*path)[len(*path)-1] = 'R'
if dfs(node.Right, x, path) {
return true
}
*path = (*path)[:len(*path)-1]
return false
}

node := lca(root, startValue, destValue)
pathToStart := []byte{}
pathToDest := []byte{}
dfs(node, startValue, &pathToStart)
dfs(node, destValue, &pathToDest)
return string(bytes.Repeat([]byte{'U'}, len(pathToStart))) + string(pathToDest)
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,41 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private Map<Integer, List<List<String>>> edges;
private Set<Integer> visited;
private String ans;

public String getDirections(TreeNode root, int startValue, int destValue) {
edges = new HashMap<>();
visited = new HashSet<>();
ans = null;
traverse(root);
dfs(startValue, destValue, new ArrayList<>());
return ans;
TreeNode node = lca(root, startValue, destValue);
StringBuilder pathToStart = new StringBuilder();
StringBuilder pathToDest = new StringBuilder();
dfs(node, startValue, pathToStart);
dfs(node, destValue, pathToDest);
return "U".repeat(pathToStart.length()) + pathToDest.toString();
}

private void traverse(TreeNode root) {
if (root == null) {
return;
}
if (root.left != null) {
edges.computeIfAbsent(root.val, k -> new ArrayList<>())
.add(Arrays.asList(String.valueOf(root.left.val), "L"));
edges.computeIfAbsent(root.left.val, k -> new ArrayList<>())
.add(Arrays.asList(String.valueOf(root.val), "U"));
private TreeNode lca(TreeNode node, int p, int q) {
if (node == null || node.val == p || node.val == q) {
return node;
}
if (root.right != null) {
edges.computeIfAbsent(root.val, k -> new ArrayList<>())
.add(Arrays.asList(String.valueOf(root.right.val), "R"));
edges.computeIfAbsent(root.right.val, k -> new ArrayList<>())
.add(Arrays.asList(String.valueOf(root.val), "U"));
TreeNode left = lca(node.left, p, q);
TreeNode right = lca(node.right, p, q);
if (left != null && right != null) {
return node;
}
traverse(root.left);
traverse(root.right);
return left != null ? left : right;
}

private void dfs(int start, int dest, List<String> t) {
if (visited.contains(start)) {
return;
private boolean dfs(TreeNode node, int x, StringBuilder path) {
if (node == null) {
return false;
}
if (node.val == x) {
return true;
}
if (start == dest) {
if (ans == null || ans.length() > t.size()) {
ans = String.join("", t);
}
return;
path.append('L');
if (dfs(node.left, x, path)) {
return true;
}
visited.add(start);
if (edges.containsKey(start)) {
for (List<String> item : edges.get(start)) {
t.add(item.get(1));
dfs(Integer.parseInt(item.get(0)), dest, t);
t.remove(t.size() - 1);
}
path.setCharAt(path.length() - 1, 'R');
if (dfs(node.right, x, path)) {
return true;
}
path.deleteCharAt(path.length() - 1);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@
# self.val = val
# self.left = left
# self.right = right


class Solution:
def getDirections(
self, root: Optional[TreeNode], startValue: int, destValue: int
) -> str:
edges = defaultdict(list)
ans = None
visited = set()
def lca(node: Optional[TreeNode], p: int, q: int):
if node is None or node.val in (p, q):
return node
left = lca(node.left, p, q)
right = lca(node.right, p, q)
if left and right:
return node
return left or right

def dfs(node: Optional[TreeNode], x: int, path: List[str]):
if node is None:
return False
if node.val == x:
return True
path.append("L")
if dfs(node.left, x, path):
return True
path[-1] = "R"
if dfs(node.right, x, path):
return True
path.pop()
return False

node = lca(root, startValue, destValue)

def traverse(root):
if not root:
return
if root.left:
edges[root.val].append([root.left.val, 'L'])
edges[root.left.val].append([root.val, 'U'])
if root.right:
edges[root.val].append([root.right.val, 'R'])
edges[root.right.val].append([root.val, 'U'])
traverse(root.left)
traverse(root.right)
path_to_start = []
path_to_dest = []

def dfs(start, dest, t):
nonlocal ans
if start in visited:
return
if start == dest:
if ans is None or len(ans) > len(t):
ans = ''.join(t)
return
visited.add(start)
for d, k in edges[start]:
t.append(k)
dfs(d, dest, t)
t.pop()
dfs(node, startValue, path_to_start)
dfs(node, destValue, path_to_dest)

traverse(root)
dfs(startValue, destValue, [])
return ans
return "U" * len(path_to_start) + "".join(path_to_dest)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function getDirections(root: TreeNode | null, startValue: number, destValue: number): string {
const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => {
if (node === null || node.val === p || node.val === q) {
return node;
}
const left = lca(node.left, p, q);
const right = lca(node.right, p, q);
if (left !== null && right !== null) {
return node;
}
return left !== null ? left : right;
};

const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => {
if (node === null) {
return false;
}
if (node.val === x) {
return true;
}
path.push('L');
if (dfs(node.left, x, path)) {
return true;
}
path[path.length - 1] = 'R';
if (dfs(node.right, x, path)) {
return true;
}
path.pop();
return false;
};

const node = lca(root, startValue, destValue);
const pathToStart: string[] = [];
const pathToDest: string[] = [];
dfs(node, startValue, pathToStart);
dfs(node, destValue, pathToDest);
return 'U'.repeat(pathToStart.length) + pathToDest.join('');
}
Loading
Loading