Breadth First Search Algorithm to Find Nearest Right Node in Bin

  • 时间:2020-10-07 14:14:07
  • 分类:网络文摘
  • 阅读:132 次

Given the root of a binary tree and a node u in the tree, return the nearest node on the same level that is to the right of u, or return null if u is the rightmost node in its level.

binary-tree-nearest-right-node Breadth First Search Algorithm to Find Nearest Right Node in Binary Tree algorithms BFS

Example 1:
Input: root = [1,2,3,null,4,5,6], u = 4
Output: 5
Explanation: The nearest node on the same level to the right of node 4 is node 5.

Example 2:
Input: root = [3,null,4,2], u = 2
Output: null
Explanation: There are no nodes to the right of 2.

Example 3:
Input: root = [1], u = 1
Output: null

Example 4:
Input: root = [3,4,2,null,null,null,1], u = 4
Output: 2

Constraints:
The number of nodes in the tree is in the range [1, 105].
1 <= Node.val <= 105
All values in the tree are distinct.
u is a node in the binary tree rooted at root.

Find Nearest Right Node in Binary Tree using BFS Algorithm

We know that the Breadth First Search Algorithm (BFS) traverses the tree/graph level-by-level. Thus, we know when we meet a given node – its next right neigbour on the same level. This is do-able by expanding the children nodes all at once.

At each iteration – we record the number of the nodes in the queue – which is the number of the total nodes that are all in the same level.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * 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:
    TreeNode* findNeartestRightNode(TreeNode* root, TreeNode* u) {
        if (!root) return NULL;
        queue<TreeNode*> Q;
        Q.push(root);
        while (!Q.empty()) {
            auto sz = Q.size();
            for (int i = 0; i < sz; ++ i) {
                auto p = Q.front();
                Q.pop();
                if (p == u) { // given node
                    if (i == sz - 1) { // if we don't have right node(s)
                        return NULL;
                    }
                    return Q.front();
                }
                if (p->left) Q.push(p->left);
                if (p->right) Q.push(p->right);
            }                
        }
        return NULL;
    }
};
/**
 * 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:
    TreeNode* findNeartestRightNode(TreeNode* root, TreeNode* u) {
        if (!root) return NULL;
        queue<TreeNode*> Q;
        Q.push(root);
        while (!Q.empty()) {
            auto sz = Q.size();
            for (int i = 0; i < sz; ++ i) {
                auto p = Q.front();
                Q.pop();
                if (p == u) { // given node
                    if (i == sz - 1) { // if we don't have right node(s)
                        return NULL;
                    }
                    return Q.front();
                }
                if (p->left) Q.push(p->left);
                if (p->right) Q.push(p->right);
            }                
        }
        return NULL;
    }
};

The runtime complexity is O(N) where N is the number of the nodes in the binary tree and the space requirement is also O(N) as we are using a queue to perform the Breadth First Search Algorithm.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
邹忌讽齐王纳谏原文及翻译  范雎说秦王原文及翻译  三个博客写作技巧坚持了10年 养活了他一家子!  百度搜索正式升级冰桶算法5.0  现在的建站公司都有哪些套路?真会吹!  百度反推算法,又一次站长和百度之间的较量  百度搜索低调改版搜索界面  STUSEO研究社Nico:seo的核心思想和seo影响因素综合分析  委托建站公司搭建的网站,会涉嫌源码侵权?  99%的网站死于太丑!!! 
评论列表
添加评论