zoukankan      html  css  js  c++  java
  • LeetCode 742. Closest Leaf in a Binary Tree

    原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/

    题目:

    Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree.

    Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.

    In the following examples, the input tree is represented in flattened form row by row. The actual root tree given will be a TreeNode object.

    Example 1:

    Input:
    root = [1, 3, 2], k = 1
    Diagram of binary tree:
              1
             / 
            3   2
    
    Output: 2 (or 3)
    
    Explanation: Either 2 or 3 is the nearest leaf node to the target of 1.
    

    Example 2:

    Input:
    root = [1], k = 1
    Output: 1
    
    Explanation: The nearest leaf node is the root node itself.
    

    Example 3:

    Input:
    root = [1,2,3,4,null,null,null,5,null,6], k = 2
    Diagram of binary tree:
                 1
                / 
               2   3
              /
             4
            /
           5
          /
         6
    
    Output: 3
    Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2.

    Note:

    1. root represents a binary tree with at least 1 node and at most 1000 nodes.
    2. Every node has a unique node.val in range [1, 1000].
    3. There exists some node in the given binary tree for which node.val == k.

    题解:

    First do DFS, find the TreeNode whose value is k, mark as kNode. At the same time, update HashMap with child->parent relationship.

    Then do BFS from kNode, if any of left child, right child and parent is not null, put it into the queue. The first met leaf node is the nearest leaf node to kNode.

    Time Complexity: O(n).

    Space: O(n).

    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(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     TreeNode target;
    12     
    13     public int findClosestLeaf(TreeNode root, int k) {
    14         if(root == null){
    15             return -1;
    16         }
    17         
    18         HashMap<TreeNode, TreeNode> nodeToPar = new HashMap<>();
    19         dfs(root, null, nodeToPar, k);
    20         
    21         if(target == null){
    22             return -1;
    23         }
    24         
    25         LinkedList<TreeNode> que = new LinkedList<>();
    26         HashSet<Integer> visited = new HashSet<>();
    27         que.add(target);
    28         visited.add(k);
    29         
    30         while(!que.isEmpty()){
    31             TreeNode cur = que.poll();
    32             if(cur.left == null && cur.right == null){
    33                 return cur.val;
    34             }
    35             
    36             if(cur.left != null && !visited.contains(cur.left.val)){
    37                 que.add(cur.left);
    38                 visited.add(cur.left.val);
    39             }
    40             
    41             if(cur.right != null && !visited.contains(cur.right.val)){
    42                 que.add(cur.right);
    43                 visited.add(cur.right.val);
    44             }
    45             
    46             if(nodeToPar.containsKey(cur) && !visited.contains(nodeToPar.get(cur).val)){
    47                 que.add(nodeToPar.get(cur));
    48                 visited.add(nodeToPar.get(cur).val);
    49             }
    50         }
    51         
    52         return -1;
    53     }
    54     
    55     private void dfs(TreeNode root, TreeNode parent, HashMap<TreeNode, TreeNode> nodeToPar, int k){
    56         if(root == null){
    57             return;
    58         }
    59         
    60         if(parent != null){
    61             nodeToPar.put(root, parent);
    62         }
    63         
    64         if(root.val == k){
    65             target = root;
    66         }
    67         
    68         dfs(root.left, root, nodeToPar, k);
    69         dfs(root.right, root, nodeToPar, k);
    70     }
    71 }
  • 相关阅读:
    关于模式窗体的缓存问题的解决方案
    C# 读取网页
    C# 压缩文件
    C#实现反射调用动态加载的DLL文件中的方法
    在线程中修改窗体控件内容
    C# 启用双缓存,避免ListView控件加载数据时闪烁
    JBuilder2005破解方法
    C# combbox datatable 赋值
    今天感觉到秋凉了~
    烦人的流程图~~~
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11008908.html
Copyright © 2011-2022 走看看