首页 > 其他分享 >从二叉树根节点搜索到指定结点的路径

从二叉树根节点搜索到指定结点的路径

时间:2022-08-13 19:14:26浏览次数:53  
标签:结点 target findPath 节点 二叉树 found root stack

LC 236题,二叉树的最近公共祖先:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/

其中一种解法的关键,是找到从根节点到指定结点的路径。

 

    public static boolean findPath(TreeNode root, TreeNode target, Stack<TreeNode> stack){
        boolean found = false;
        if (root != null){
            if (root.val == target.val || findPath(root.left, target, stack) || findPath(root.right, target, stack)){
                stack.push(root);
                found = true;
            }
        }
        return found;
    }

 

标签:结点,target,findPath,节点,二叉树,found,root,stack
From: https://www.cnblogs.com/jacksonshi/p/16583817.html

相关文章

  • 010Zookeeper的四种节点类型
    节点有两个维度,一个是永久的还是临时的,另一个是否有序。组合成的四种类型如下:1:PERSISTENT//持久化节点2:PERSISTENT_SEQUENTIAL//持久化排序节点3:EPHEMERAL//临时......