Leetcode 3701 · Find Nearest Right Node in Binary Tree (遍历和BFS好题)

3701 · Find Nearest Right Node in Binary TreePRE
Algorithms

This topic is a pre-release topic. If you encounter any problems, please contact us via “Problem Correction”, and we will upgrade your account to VIP as a thank you.
Description
Given a binary tree with a root node root and a node u in the tree, return the node value val of the right-hand node nearest to it in the layer in which the node u is located, or -1 if the node u is the rightmost node in the current layer.

The total number of nodes is between

All nodes in the tree have node values that are unique
u is a node in a binary tree rooted at root

Example
Example 1:

Input:
root = {1,2,3,#,4,5,6}
u = 2
Output:
3
Explanation:
As shown in the figure, in the layer where node 2 is located, the nearest right node is node 3
3701_1.png

Example 2:

Input:
root = {3,1,#,#,2}
u = 1
Output:
-1
Explanation:
As shown in the figure, the layer where node 1 is located has only one node and the nearest right node is not found

解法1:遍历
采用前序遍历。当找到目标节点后,记住当前层次。那么,前序遍历再次到达该层次的时候,访问到的节点就是所求节点。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
   
public:
    /**
     * @param root: The root of the binary tree
     * @param u: A node in root
     * @return: Node value of the right node
     */
    int findNearestRightNode(TreeNode *root, TreeNode *u) {
   
        helper(root, 0, u);
        if (findNode) return findNode->val;
        return -1;
    }
private:
    bool find = false;
    int targetDepth = -1;
    TreeNode *findNode = NULL;
    void helper(TreeNode *root, int depth, TreeNode *u) {
   
        if (!root) return;
        if (findNode) return;
        if (find && depth == targetDepth) {
   
            findNode = root;
            return;
        }
        if (root == u) {
   
            find = true;
            targetDepth = depth;
        }
        helper(root->left, depth + 1, u);
        helper(root->right, depth + 1, u);
    }
};

写成这样也可以。这样就不用find变量了。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
   
public:
    /**
     * @param root: The root of the binary tree
     * @param u: A node in root
     * @return: Node value of the right node
     */
    int findNearestRightNode(TreeNode *root, TreeNode *u) {
   
        helper(root, 0, u);
        if (findNode) return findNode->val;
        return -1;
    }
private:
    int targetDepth = -1;
    TreeNode *findNode = NULL;
    void helper(TreeNode *root, int depth, TreeNode *u) {
   
        if (!root || findNode) return;
        if (root == u) {
   
            targetDepth = depth;
        } else if (targetDepth == depth) {
   
            findNode = root;
            return;
        }
        helper(root->left, depth + 1, u);
        helper(root->right, depth + 1, u);
    }
};

注意: 下面这个写法不对。
只用find是不够的,因为还有些其它同层次的节点在处理,结果会覆盖resValue。
比如说输入:root = [1,2,3,null,4,5,6], u = 4
下面会输出:6。但结果应该是5。

class Solution {
   
public:
    /**
     * @param root: The root of the binary tree
     * @param u: A node in root
     * @return: Node value of the right node
     */
    int findNearestRightNode(TreeNode *root, TreeNode *u) {
   
        helper(root, 0, u);
        return resValue;
    }
private:
    bool find = false;
    int resValue = -1, targetDepth = -1;
    void helper(TreeNode *root, int depth, TreeNode *u) {
   
        if (!root) return;
        if (find && depth == targetDepth) {
   
            resValue = root->val;
            return;
        }
        if (root == u) {
   
            find = true;
            targetDepth = depth;
        }
        helper(root->left, depth + 1, u);
        helper(root->right, depth + 1, u);
    }
};

解法2: bfs

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
   
public:
    /**
     * @param root: The root of the binary tree
     * @param u: A node in root
     * @return: Node value of the right node
     */
    int findNearestRightNode(TreeNode *root, TreeNode *u) {
   
        if (!root ||! u) return -1;
        queue<TreeNode *> q;
        int res = -1;
        bool find = false;
        q.push(root);
        while(!q.empty()) {
   
            int qSize = q.size();
            find = false;
            for (int i = 0; i < qSize; i++) {
   
                TreeNode *frontNode = q.front();
                  q.pop();
                if (find) return frontNode->val;
                if (frontNode == u) find = true;
                if (frontNode->left) q.push(frontNode->left);
                if (frontNode->right) q.push(frontNode->right);
            }
        }
        return -1;
    }
};

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 06:12:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 06:12:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 06:12:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 06:12:06       20 阅读

热门阅读

  1. #{}和${}的区别?

    2024-01-10 06:12:06       23 阅读
  2. 离线安装docker和docker-compose

    2024-01-10 06:12:06       40 阅读
  3. 深度学习中Epoch和Batch Size的关系

    2024-01-10 06:12:06       35 阅读
  4. 树莓派Debian系统中如何用mDNS广播自己的ip地址

    2024-01-10 06:12:06       34 阅读
  5. [力扣 Hot100]Day1 两数之和

    2024-01-10 06:12:06       41 阅读
  6. 【docker】docker-compose.yml 语法详解

    2024-01-10 06:12:06       34 阅读
  7. nginx upstream负载均衡模块

    2024-01-10 06:12:06       28 阅读
  8. xcode 14.3升级 pod升级

    2024-01-10 06:12:06       29 阅读
  9. Hive之set参数大全-2

    2024-01-10 06:12:06       27 阅读
  10. qt-day2

    qt-day2

    2024-01-10 06:12:06      31 阅读
  11. xcode-docC

    2024-01-10 06:12:06       35 阅读
  12. Hive之函数解析

    2024-01-10 06:12:06       28 阅读
  13. 与AI合作 -- 单例工厂2遗留的问题:bard的错误

    2024-01-10 06:12:06       33 阅读
  14. 【力扣100】【好题】155.最小栈

    2024-01-10 06:12:06       40 阅读
  15. ES6规范

    2024-01-10 06:12:06       28 阅读