Skip to content

对于树类题目, 状态存储在当前节点的返回值, 递归到每个节点的返回值都是当前节点的值和左子树与右子树的返回

java
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}
// @lc code=start

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
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 if (left == null && right != null) return right;
        else return left;
    }
}

Personal Knowledge Base