剑指 Offer 64. 求1+2+…+n
题目
代码
class Solution {
int res=0;
public int sumNums(int n) {
// 使用逻辑运算符的短路属性;A&&B逻辑与:当A为false的时候,B的真假就不用判断,直接返回false
// A||B 逻辑或:当A为真的时候,B的真假就不会计算,直接返回True的结果
// 使用递归进行加sum运算,使用逻辑运算符的短路属性判断是否终止
return recur(n);
}
int recur(int n){
boolean x=n>1&&recur(n-1)>0;
res+=n;
return res;
}
}
class Solution:
def sumNums(self, n: int) -> int:
# // 使用逻辑运算符的短路属性;A&&B逻辑与:当A为false的时候,B的真假就不用判断,直接返回false
# // A||B 逻辑或:当A为真的时候,B的真假就不会计算,直接返回True的结果
# // 使用递归进行加sum运算,使用逻辑运算符的短路属性判断是否终止
def recur(n):
# 使用逻辑判断and关键词,充当递归的终止条件
n>1 and recur(n-1)>0
self.res+=n
return self.res
self.res=0
recur(n)
return self.res
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
题目
代码
/**
* 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) {
// 解题思路:平衡二叉树,通过大小判断p与q在root的哪一侧,若为不同侧,直接返回root即为最近公共节点
// while(root!=null){
// if(root.val<p.val&&root.val<q.val) root=root.right;
// else if(root.val>p.val&&root.val>q.val) root=root.left;
// else return root;
// }
// return null;
// 递归方法:
return recur(root,p,q);
}
TreeNode recur(TreeNode root, TreeNode p, TreeNode q){
if(root==null) return null;
if(root.val<p.val&&root.val<q.val) return recur(root.right,p,q);
if(root.val>p.val&&root.val>q.val) return recur(root.left,p,q);
else return root;
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 使用循环法:
# while root:
# if root.val>p.val and root.val>q.val: root=root.left
# elif root.val<p.val and root.val<q.val: root=root.right
# else: break
# return root
# 使用递归方法
def recur(root,p,q):
if not root: return None
if root.val>p.val and root.val>q.val: return recur(root.left,p,q)
elif root.val<p.val and root.val<q.val: return recur(root.right,p,q)
else: return root
return None
return recur(root,p,q)
剑指 Offer 68 - II. 二叉树的最近公共祖先
题目
代码
/**
* 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||root==p||root==q) return root;
TreeNode left=lowestCommonAncestor(root.left,p,q);
TreeNode right=lowestCommonAncestor(root.right,p,q);
if(left==null) return right;
if(right==null) return left;
// 如果左右子树的返回值均不为null,则代表p和q在root的两侧,root即为最近公共祖先
return root;
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
if not root or root==p or root==q: return root
left=self.lowestCommonAncestor(root.left,p,q)
right=self.lowestCommonAncestor(root.right,p,q)
if not left: return right
if not right:return left
return root