zoukankan      html  css  js  c++  java
  • 236. Lowest Common Ancestor of a Binary Tree

    题目:

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

    According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

            _______3______
           /              
        ___5__          ___1__
       /              /      
       6      _2       0       8
             /  
             7   4
    

    For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

    链接: http://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/

    题解:

    普通二叉树求公共祖先。看过<剑指Offer>以后知道这道题应该形成一系列问题。比如是不是二叉树,是不是BST。假如是BST的话我们可以用上题的方法,二分搜索。有没有指向父节点的link,假如有指向父节点的link我们就可以用intersection of two lists的方法找到两个linked list相交的地方。 对这道题目,我们使用后续遍历来做:

    1. 定义两个辅助节点,使用后续遍历来遍历整个树
    2. 当root的值等于p或者q时,找到一个符合条件的节点,返回这个root
    3. 先遍历左子树
    4. 再遍历右子树
    5. 当left,right均找到时返回此root
    6. 只找到left时返回left
    7. 只找到right时返回right
    8. 否则返回null

    Time Complexity - O(n), Space Complexity - O(n)

    public class Solution {
        public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null) 
                return null;
            if (root == p || root == q) 
                return root;
            
            TreeNode left = lowestCommonAncestor(root.left, p, q);      // Post order traveral
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            
            if (left != null && right != null)          // p and q in two subtrees
                return root;
            else
                return left != null ? left : right; 
        }
    }

    二刷:

    Java:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null || root == p || root == q) return root;
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if (left != null && right != null) return root;
            else return left != null ? left : right;
        }
    }

    三刷

    还是跟以前一样的方法,利用递归,先遍历两个子树,来查找是否其中含有目标节点p或者q。假如两节点分别位于root的左右两侧,则root为LCA. 否则,left和right哪个非空,则哪一个为LCA, 这一侧含有p和q两个目标节点。

    Java:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null) return null;
            if (root == p || root == q) return root;
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if (left != null && right != null) return root;
            else return (left != null) ? left : right;
        }
    }

    Reference:

  • 相关阅读:
    [LUPA学习向导]如何成为Linux平台C语言程序员
    C程序调用shell脚本共有三种方法
    用C语言编写一个Linux下的简单shell程序
    利用sscanf()匹配字符串需要注意贪婪匹配
    对于初学者来说,选择合适的开发板非常之非常重要呀
    迅为IMX6ULL开发板C程序调用shell
    迅为-iMX6ULL开发板上配置AP热点
    迅为iMX6开发板-设备树内核-menuconfig的使用
    双11买它阿,零基础配套视频教程4412开发板嵌入式入门
    迅为-iMX6ULL开发板-移植mjpg-streamer实现远程监控
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5003803.html
Copyright © 2011-2022 走看看