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

原题链接在这里:https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/

题目:

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

题解:

First find the path from root to stat node. "LLRR"

Find the path from root to destination node. "LRR"

Delete the common prefix, which is "L" and we get "LRR" and "RR".

Repalce start with "UUU", and append "RR". We get "UUURR".

DFS returns if we could find the target value node.

Its state needs current root, target val and StringBuilder.

Note: In DFS, it is if and else if. Since it is either left or right subtree.

Time Complexity: O(n). n is size of tree.

Space: O(logn).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public String getDirections(TreeNode root, int startValue, int destValue) {
18         if(root == null){
19             return "";
20         }
21         
22         StringBuilder sbStart = new StringBuilder();
23         StringBuilder sbDest = new StringBuilder();
24         dfs(root, startValue, sbStart);
25         dfs(root, destValue, sbDest);
26         int i = 0; 
27         int startLen = sbStart.length();
28         int destLen = sbDest.length();
29         int len = Math.min(startLen, destLen);
30         while(i < len && sbStart.charAt(startLen - 1 - i) == sbDest.charAt(destLen - 1- i)){
31             i++;
32         }
33         
34         return "U".repeat(sbStart.length() - i) + sbDest.reverse().toString().substring(i);
35     }
36     
37     private boolean dfs(TreeNode root, int val, StringBuilder sb){
38         if(root.val == val){
39             return true;
40         }
41         
42         if(root.left != null && dfs(root.left, val, sb)){
43             sb.append("L");
44         }else if(root.right != null && dfs(root.right, val, sb)){
45             sb.append("R");
46         }
47         
48         return sb.length() > 0;
49     }
50 }

类似Find Distance in a Binary Tree.

posted @ 2022-08-12 15:35  Dylan_Java_NYC  阅读(168)  评论(0编辑  收藏  举报