返回介绍

solution / 2000-2099 / 2096.Step-By-Step Directions From a Binary Tree Node to Another / README_EN

发布于 2024-06-17 01:03:10 字数 7831 浏览 0 评论 0 收藏 0

2096. Step-By-Step Directions From a Binary Tree Node to Another

中文文档

Description

You are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue representing the value of the start node s, and a different integer destValue representing the value of the destination node t.

Find the shortest path starting from node s and ending at node t. Generate step-by-step directions of such path as a string consisting of only the uppercase letters 'L', 'R', and 'U'. Each letter indicates a specific direction:

  • 'L' means to go from a node to its left child node.
  • 'R' means to go from a node to its right child node.
  • 'U' means to go from a node to its parent node.

Return _the step-by-step directions of the shortest path from node _s_ to node_ t.

 

Example 1:

Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
Output: "UURL"
Explanation: The shortest path is: 3 → 1 → 5 → 2 → 6.

Example 2:

Input: root = [2,1], startValue = 2, destValue = 1
Output: "L"
Explanation: The shortest path is: 2 → 1.

 

Constraints:

  • The number of nodes in the tree is n.
  • 2 <= n <= 105
  • 1 <= Node.val <= n
  • All the values in the tree are unique.
  • 1 <= startValue, destValue <= n
  • startValue != destValue

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     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 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)

    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()

    traverse(root)
    dfs(startValue, destValue, [])
    return ans
/**
 * 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;
  }

  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"));
    }
    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"));
    }
    traverse(root.left);
    traverse(root.right);
  }

  private void dfs(int start, int dest, List<String> t) {
    if (visited.contains(start)) {
      return;
    }
    if (start == dest) {
      if (ans == null || ans.length() > t.size()) {
        ans = String.join("", t);
      }
      return;
    }
    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);
      }
    }
  }
}
/**
 * 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:
  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;
  }

  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'});
    }
    if (root->right) {
      edges[root->val].push_back({root->right->val, 'R'});
      edges[root->right->val].push_back({root->val, 'U'});
    }
    traverse(root->left);
    traverse(root->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;
    }
    visited.insert(start);
    if (edges.count(start)) {
      for (auto& item : edges[start]) {
        t += item.second;
        dfs(item.first, dest, t);
        t.pop_back();
      }
    }
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文