标签:nan ret root 递归 turn col bsp public 回溯
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ //递归 回溯 class Solution { TreeNode res=null; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { dfs(root,p,q); return res; } private int dfs(TreeNode root,TreeNode p, TreeNode q){ if(root==null)return 0; int left=dfs(root.left,p,q); int right=dfs(root.right,p,q); int mid=root==p||root==q?1:0; if(left+right+mid>1)res=root; return left+right+mid>0?1:0; } }
标签:nan ret root 递归 turn col bsp public 回溯
原文地址:https://www.cnblogs.com/NeverGiveUp0/p/11140117.html