close
The Wayback Machine - https://web.archive.org/web/20201015042719/https://github.com/sisterAn/JavaScript-Algorithms/issues/82
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

字节一面:给定一个二叉树, 找到该树中两个指定节点间的最短距离 #82

Open
sisterAn opened this issue Jul 13, 2020 · 2 comments
Labels

Comments

@sisterAn
Copy link
Owner

@sisterAn sisterAn commented Jul 13, 2020

function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}

@sisterAn sisterAn added the 字节 label Jul 13, 2020
@sisterAn sisterAn changed the title 字节一面:二叉树中两个节点间的最短距离 字节一面:给定一个二叉树, 找到该树中两个指定节点间的最短距离 Jul 13, 2020
@7777sea
Copy link

@7777sea 7777sea commented Jul 14, 2020

先找出两个节点的最近公共祖先
分别求出两个节点到最近公共祖先的路径长度
求出两个节点的路径长度

求公共祖先

var lowestCommonAncestor = function(root, p, q) { 
    if (root===null||root===p||root===q) {
        return root
    }
    let left = lowestCommonAncestor(root.left, p, q)
    let right = lowestCommonAncestor(root.right, p, q)
    if (left && right) {
        return root
    }
    return left || right
};

计算距离

let visited = false
let stack = []
var getDisToPar = function(root, p, stack) {
    if(root==null){
         return ;
     }
    //将节点添加到栈中
     stack.push(root.val);
    //如果找到了
    if(!visited&&root==p){
        visited  = true;
        return;
     }
    //先找左子树
    if(!visited){
        getDisToPar(root.left,p,stack);
    }
    //左子树没找到再找右子树
    if(!visited){
        getDisToPar(root.right,p,stack);
    }
    //如果还没找到,说明不在这个节点下面,弹出来
    if(!visited){
        stack.pop();
    }
    return;
}
@sisterAn
Copy link
Owner Author

@sisterAn sisterAn commented Jul 14, 2020

解答:

求最近公共祖先节点,然后再求最近公共祖先节点到两个指定节点的路径,再求两个节点的路径之和

const shortestDistance = function(root, p, q) {
    // 最近公共祖先
    let lowestCA = lowestCommonAncestor(root, p, q)
    // 分别求出公共祖先到两个节点的路经
    let pDis = [], qDis = []
    getPath(lowestCA, p, pDis)
    getPath(lowestCA, q, qDis)
    // 返回路径之和
    return (pDis.length + qDis.length)
}

// 最近公共祖先
const lowestCommonAncestor = function(root, p, q) {
    if(root === null || root === p || root === q) return root
    const left = lowestCommonAncestor(root.left, p, q)
    const right = lowestCommonAncestor(root.right, p, q)
    if(left === null) return right
    if(right === null) return left
    return root
}

const getPath = function(root, p, paths) {
    // 找到节点,返回 true
    if(root === p) return true
    // 当前节点加入路径中
    paths.push(root)
    let hasFound = false
    // 先找左子树
    if (root.left !== null)
        hasFound = getPath(root.left, p, paths)
    // 左子树没有找到,再找右子树
    if (!hasFound && root.right !== null)
        hasFound = getPath(root.right, p, paths)
    // 没有找到,说明不在这个节点下面,则弹出
    if (!hasFound)
        paths.pop()
    return hasFound
}
@Linjiayu6 Linjiayu6 mentioned this issue Jul 19, 2020
4 of 9 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.