zoukankan      html  css  js  c++  java
  • Leetcode: 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:
    root represents a binary tree with at least 1 node and at most 1000 nodes.
    Every node has a unique node.val in range [1, 1000].
    There exists some node in the given binary tree for which node.val == k.

    Tree -----> Graph

    1. First, preform DFS on root in order to find the node whose val = k, at the meantime use HashMap to keep record of all back edges from child to parent;
    2. Then perform BFS on this node to find the closest leaf node.

    Note only the nodes visited in DFS are put into the backedgeMap, the others don't. This is fine, cause only from KNode to root this path, we need to check backMap.

     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     public int findClosestLeaf(TreeNode root, int k) {
    12         HashMap<TreeNode, TreeNode> backMap = new HashMap<>(); // store all edges that trace node back to its parent
    13         HashSet<TreeNode> visited = new HashSet<>(); // visited node in BFS
    14         Queue<TreeNode> queue = new LinkedList<>(); // for BFS
    15         
    16         TreeNode KNode = dfs(root, k, backMap); // DFS: search for node whoes val == k
    17         
    18         queue.offer(KNode);
    19         visited.add(KNode);
    20         
    21         while (!queue.isEmpty()) {
    22             TreeNode cur = queue.poll();
    23             if (cur.left == null && cur.right == null) {
    24                 return cur.val;
    25             }
    26             if (cur.left != null && visited.add(cur.left)) {
    27                 queue.offer(cur.left);
    28             }
    29             if (cur.right != null && visited.add(cur.right)) {
    30                 queue.offer(cur.right);
    31             }
    32             if (backMap.containsKey(cur) && visited.add(backMap.get(cur))) {
    33                 queue.offer(backMap.get(cur));
    34             }
    35         }
    36         
    37         return -1;
    38     }
    39     
    40     public TreeNode dfs(TreeNode cur, int k, HashMap<TreeNode, TreeNode> backMap) {
    41         if (cur.val == k) {
    42             return cur;
    43         }
    44         if (cur.left != null) {
    45             TreeNode left = dfs(cur.left, k, backMap);
    46             backMap.put(cur.left, cur);
    47             if (left != null) return left;
    48         }
    49         if (cur.right != null) {
    50             TreeNode right = dfs(cur.right, k, backMap);
    51             backMap.put(cur.right, cur);
    52             if (right != null) return right;
    53         }
    54         return null;
    55     }
    56 }
  • 相关阅读:
    .net日期类与UNIX时间戳的相互转换,长数字
    钉钉的生日模块在哪
    js判断手机是苹果(IOS)还是安卓(android) H5手机端自适应宽高
    .net网站部署winserver2008R2 IIS只列出目录 浏览只显示目录浏览
    ajax有时请求不到数据 后台,有时收不到返回值的解决办法
    overflow不超出时不显示滚动条 属性解决内容未超出依然显示滚动条轨道的问题
    PB取datawindow生成的语句。要在datawindow的sqlpreview事件
    电脑C盘缓存路径在哪,清理C盘哪个文件夹可以删
    PB里执行写SQL语句
    SQL SERVER合并行。将多行数据合并成一行,字符串拼接
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/11769641.html
Copyright © 2011-2022 走看看