LeetCode 1740. Find Distance in a Binary Tree
原题链接在这里:https://leetcode.com/problems/find-distance-in-a-binary-tree/
题目:
Given the root of a binary tree and two integers p
and q
, return the distance between the nodes of value p
and value q
in the tree.
The distance between two nodes is the number of edges on the path from one to the other.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 0 Output: 3 Explanation: There are 3 edges between 5 and 0: 5-3-1-0.
Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 7 Output: 2 Explanation: There are 2 edges between 5 and 7: 5-2-7.
Example 3:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 5 Output: 0 Explanation: The distance between a node and itself is 0.
Constraints:
- The number of nodes in the tree is in the range
[1, 104]
. 0 <= Node.val <= 109
- All
Node.val
are unique. p
andq
are values in the tree.
题解:
We could find the LCA.
And do a BFS from LCA to p and q to calculate the distance.
We could it in one pass as well.
DFS returns current root distance to either p or q.
When root.val == p || q. One case is it is the first tiem we see p or q and didn't see the other, we return 0.
The other case is it is the second time we see p or q, which means we have seen both p and q. We find the LCA, it is either p or q. Return Math.max(l, r) + 1.
If both l >=0 && r >= 0, it means we find the LCA, its subtrees contains p and q. Return l + r + 2.
Time Complexity: O(n). n is the 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 int res = -1; 18 public int findDistance(TreeNode root, int p, int q) { 19 if(p == q){ 20 return 0; 21 } 22 23 dfs(root, p, q); 24 return res; 25 } 26 27 private int dfs(TreeNode root, int p, int q){ 28 if(root == null){ 29 return -1; 30 } 31 32 int l = dfs(root.left, p, q); 33 int r = dfs(root.right, p, q); 34 if(root.val == p || root.val == q){ 35 if(l < 0 && r < 0){ 36 return 0; 37 } 38 39 // we find the LCA. 40 res = Math.max(l, r) + 1; 41 return -1; 42 } 43 44 if(l >= 0 && r >= 0){ 45 // we find the LCA. 46 res = l + r + 2; 47 return -1; 48 } 49 50 if(l >= 0){ 51 return l + 1; 52 } 53 54 if(r >= 0){ 55 return r + 1; 56 } 57 58 return -1; 59 } 60 }
AC Python:
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def findDistance(self, root: Optional[TreeNode], p: int, q: int) -> int: 9 def findLca(node, p, q): 10 if not node or node.val == p or node.val == q: 11 return node 12 left = findLca(node.left, p, q) 13 right = findLca(node.right, p, q) 14 if left and right: 15 return node 16 return left if left else right 17 18 def findDis(node, target, dis): 19 if not node: 20 return -1 21 if node.val == target: 22 return dis 23 24 left = findDis(node.left, target, dis + 1) 25 right = findDis(node.right, target, dis + 1) 26 if left != -1: 27 return left 28 if right != -1: 29 return right 30 return -1 31 32 lca = findLca(root, p, q) 33 disP = findDis(lca, p, 0) 34 disQ = findDis(lca, q, 0) 35 return disP + disQ
类似Lowest Common Ancestor of a Binary Tree, Step-By-Step Directions From a Binary Tree Node to Another.