LeetCode刷题笔记18
面试题 04.05. 合法二叉搜索树
题目描述
实现一个函数,检查一棵二叉树是否为二叉搜索树。
思路
分别判断左右子树是否满足情况即可,左子树的任何一个节点不能大于root.val,并且每个左子树节点不能大于父节点的值,所以判断每个子节点是否符合条件,用max_val和min_val控制
代码
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
def helper(root,min_val,max_val):
if root==None:
return True
if root.val>=max_val or root.val<=min_val:
return False
return helper(root.left,min_val,root.val) and helper(root.right,root.val,max_val)
return helper(root,-float('inf'),float('inf'))
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean helper(TreeNode root,long min_val,long max_val){
if (root==null) return true;
if (root.val<=min_val || root.val>=max_val) return false;
return helper(root.left,min_val,root.val) && helper(root.right,root.val,max_val);
}
public boolean isValidBST(TreeNode root) {
return helper(root,Long.MIN_VALUE,Long.MAX_VALUE);
}
}
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool helper(TreeNode* root,long min_val,long max_val){
if (root==NULL) return true;
if (root->val<=min_val || root->val>=max_val) return false;
//bool left=true,right=true;
return helper(root->left,min_val,root->val) && helper(root->right,root->val,max_val);
}
bool isValidBST(TreeNode* root) {
return helper(root,LONG_MIN,LONG_MAX);
}
};
1448. 统计二叉树中好节点的数目
题目描述
给你一棵根为 root 的二叉树,请你返回二叉树中好节点的数目。
「好节点」X 定义为:从根到该节点 X 所经过的节点中,没有任何节点的值大于 X 的值。
思路
逐个节点遍历,传入函数中的max_n控制当前节点之前路径中的最大值
代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def goodNodes(self, root):
"""
:type root: TreeNode
:rtype: int
"""
#count=0
def helper(root,max_n):
if root==None:
return 0
count=0
if root.val>=max_n:
count+=1
if root.left:
count+=helper(root.left,max(max_n,root.val))
if root.right:
count+=helper(root.right,max(max_n,root.val))
return count
count=helper(root,-float('inf'))
return count
993. 二叉树的堂兄弟节点
题目描述
在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。
如果二叉树的两个节点深度相同,但父节点不同,则它们是一对堂兄弟节点。
我们给出了具有唯一值的二叉树的根节点 root,以及树中两个不同节点的值 x 和 y。
只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true。否则,返回 false。
思路
层序遍历,并且记录下xy节点对应的父节点,然后根据定义判断即可
代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isCousins(self, root, x, y):
"""
:type root: TreeNode
:type x: int
:type y: int
:rtype: bool
"""
if root==None:
return False
if root.left==None and root.right==None:
return False
q=[root]
x_pre=None
y_pre=None
while(len(q)>0):
tmp_q=[]
for node in q:
if node.left:
tmp_q.append(node.left)
if node.left.val==x:
x_pre=node
if node.left.val==y:
y_pre=node
if node.right:
tmp_q.append(node.right)
if node.right.val==x:
x_pre=node
if node.right.val==y:
y_pre=node
q=tmp_q
if x_pre==None and y_pre!=None:
return False
if x_pre!=None and y_pre==None:
return False
if x_pre!=None and y_pre!=None:
break
if x_pre==y_pre:
return False
return True
剑指 Offer 34. 二叉树中和为某一值的路径
题目描述
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
results=[]
def helper(root,res,tmp):
if root==None:
return
tmp_res=res-root.val
tmp=tmp+[root.val]
if tmp_res==0:
if root.left==None and root.right==None:
results.append(tmp)
helper(root.left,tmp_res,tmp)
helper(root.right,tmp_res,tmp)
helper(root,sum,[])
return results