【DFS专题】-LeetCode-993. 二叉树的堂兄弟节点

993. 二叉树的堂兄弟节点

【DFS专题】-LeetCode-993. 二叉树的堂兄弟节点_第1张图片
【DFS专题】-LeetCode-993. 二叉树的堂兄弟节点_第2张图片
提示:

  • 二叉树的节点数介于 2 到 100 之间。
  • 每个节点的值都是唯一的、范围为 1 到 100 的整数。

DFS

思路

  • 题意转换:其实是让我们判断 x x x y y y 节点的深度是否相等,且判断 x x x y y y 的父节点是不是同一个节点
  • 使用 boolean dfs(TreeNode root, int target, int deepth) 来求:求节点值为 t a r g e t target target 节点的深度(自上而下)、及其父节点 ;
    • 若当前节点 root 的左、右孩子中包含 x、y,则说明当前节点 root 为其前驱节点;
    • 如果找到x、y,则立即返回
  • 使用 dfs 分别找 x、y 的深度、及其父节点;
  • 判断 x、y 是堂兄的条件为:(preX != preY) && (deepthX == deepthY)(即,不是一个爹、而且辈分还相同,就是堂兄弟 / 堂姐妹)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    TreeNode pre = null;
    TreeNode targetPre = null;
    int targetDeepth = 0;

    public boolean isCousins(TreeNode root, int x, int y) {
        if (root == null || root.val == x || root.val == y) {
            return false;
        }
        dfs(root, x, 0);
        TreeNode preX = targetPre;  // x父节点
        int deepthX = targetDeepth; // x深度
        dfs(root, y, 0);
        TreeNode preY = targetPre;  // y父节点
        int deepthY = targetDeepth; // y深度
        return (preX != preY) && (deepthX == deepthY);
    }

    // 求节点值为target的深度(自上而下)、及其父节点
    boolean dfs(TreeNode root, int target, int deepth) {
        if (root == null) return false;
        if ((root.left != null && root.left.val == target) 
            || (root.right != null && root.right.val == target)) { // 预防污染的写法
            targetDeepth = deepth;
            targetPre = root; // 更新
            return true;
        }
        if (root.left != null) { // 预防污染
            if (dfs(root.left, target, deepth + 1)) { // 找到x,直接返回
                return true;
            }
        }
        if (root.right != null) { // 预防污染
            if (dfs(root.right, target, deepth + 1)) { // 找到y,直接返回
                return true;
            }
        }
        return false;
    }
}

【DFS专题】-LeetCode-993. 二叉树的堂兄弟节点_第3张图片

出错记录

一开始记录的 pre 是“前序序列”的前驱节点,所以造成了错误…

【DFS专题】-LeetCode-993. 二叉树的堂兄弟节点_第4张图片
【DFS专题】-LeetCode-993. 二叉树的堂兄弟节点_第5张图片

出错代码记录如下

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    TreeNode pre = null;
    TreeNode targetPre = null;
    int targetDeepth = 0;

    public boolean isCousins(TreeNode root, int x, int y) {
        if (root == null || root.val == x || root.val == y) {
            return false;
        }
        dfs(root, x, 0);
        TreeNode preX = targetPre;  // x父节点
        int deepthX = targetDeepth; // x深度
        dfs(root, y, 0);
        TreeNode preY = targetPre;  // y父节点
        int deepthY = targetDeepth; // y深度
        System.out.println("preX = " + preX.val);
        System.out.println("preY = " + preY.val);
        System.out.println("deepthX = " + deepthX);
        System.out.println("deepthY = " + deepthY);
        return (preX != preY) && (deepthX == deepthY);
    }

    // 求节点值为target的深度(自上而下)、及其父节点
    void dfs(TreeNode root, int target, int deepth) {
        if (root == null) return;
        if (root.val == target) {
            targetDeepth = deepth;
            targetPre = pre; // 更新
            return;
        }
        pre = root;
        dfs(root.left, target, deepth + 1);  // 左
        dfs(root.right, target, deepth + 1); // 右
    }
}

你可能感兴趣的:(leetcode,leetcode,DFS)