设计算法查找二叉树的两个结点最近公共祖先(LCA)

    xiaoxiao2021-04-16  31

    public static BinaryTreeNode<Integer> LCA(BinaryTreeNode<Integer> root,BinaryTreeNode<Integer> a, BinaryTreeNode<Integer> b){ BinaryTreeNode<Integer> left,right; if(root==null) return root; if(root==a|| root==b) return root; left = LCA(root.getleft(),a,b); right = LCA(root.getright(),a,b); if(left!=null && right!=null) return root; else return(left!=null?left:right); }
    转载请注明原文地址: https://ju.6miu.com/read-672963.html

    最新回复(0)