94. 二叉树的中序遍历
题目
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// 递归遍历方法一:
// class Solution {
// public List<Integer> inorderTraversal(TreeNode root) {
// LinkedList<Integer> list=new LinkedList<>();
// middfs(root,list);
// return list;
// }
// public void middfs(TreeNode root,LinkedList<Integer> list){
// if(root==null) return;
// middfs(root.left,list);
// list.add(root.val);
// middfs(root.right,list);
// }
// }
// 非递归遍历二叉树
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
// list用于保存中序遍历的二叉树数值
List<Integer> list=new LinkedList<>();
// 使用辅助栈来对根节点与左子树和右子树之间顺序关系的调整
Stack<TreeNode> stack=new Stack<>();
// 赋值给当前的根节点
TreeNode cur=root;
// 开启循环cur不能为空或者栈中不为空的时候,始终循环
while(cur!=null||!stack.isEmpty()){
// 对于cur不为空的情况,寻找其左子树并将左子树压入栈中
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
// 对于上个小循环中cur为空,所以会将栈中的元素弹出为左子树的叶子节点赋值为cur
cur=stack.pop();
// list中保存左子树叶子节点
list.add(cur.val);
// 对于该叶子节点可能存在右子树,将当前cur指向右子树(如果右子树为空,则上方的小循环不会执行,cur重新赋值为栈中的最上方节点)
cur=cur.right;
}
return list;
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# 递归遍历:
# class Solution:
# def inorderTraversal(self, root: TreeNode) -> List[int]:
# def dfs(root,l):
# if not root:return
# dfs(root.left,l)
# l.append(root.val)
# dfs(root.right,l)
# l=[]
# dfs(root,l)
# return l
# 非递归遍历:
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
l=[]
stack=[]
while root or stack:
while root:
stack.append(root)
root=root.left
root=stack.pop()
l.append(root.val)
root=root.right
return l
124. 二叉树中的最大路径和
题目
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int m=Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
// 本题使用二叉树的深度遍历框架,对于最大路径可以看为max((root+root.left),(root,root.right),(root,root.left,root.right));这三种情况里的一种是
// root节点可以提供的最大路径所有情况;其中(root+root.left),(root,root.right)两种情况可以直接根据递归返回结果,但是对于最后一种情况,root仅为路径中一
// 点,所以使用max变量更新这一情况
dfs(root);
return m;
}
int dfs(TreeNode root){
// 递归结束条件
if(root==null) return 0;
// 注意应该对于其左右子树的贡献值限定在正数范围内,否则会对root为端点的小路径造成负影响,当返回0的时候就是本次函数仅返回root的情况
int left=Math.max(dfs(root.left),0);
int right=Math.max(dfs(root.right),0);
int p=left+right+root.val;
m=Math.max(m,p);
return Math.max(left,right)+root.val;
}
}
class Solution:
def __init__(self):
self.maxSum = float("-inf")
def maxPathSum(self, root: TreeNode) -> int:
def maxGain(node):
if not node:
return 0
# 递归计算左右子节点的最大贡献值
# 只有在最大贡献值大于 0 时,才会选取对应子节点
leftGain = max(maxGain(node.left), 0)
rightGain = max(maxGain(node.right), 0)
# 节点的最大路径和取决于该节点的值与该节点的左右子节点的最大贡献值
priceNewpath = node.val + leftGain + rightGain
# 更新答案
self.maxSum = max(self.maxSum, priceNewpath)
# 返回节点的最大贡献值
return node.val + max(leftGain, rightGain)
maxGain(root)
return self.maxSum
100. 相同的树
题目
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null) return true;
if(p==null||q==null) return false;
// 记住此处不应该判断两者p与q的值是否相等,因为要是相等的话会直接return true;不会递归其子树
if(p.val!=q.val) return false;
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if not p and not q:return True
if not p or not q: return False
if p.val!=q.val:return False
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
98. 验证二叉搜索树
题目
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
boolean res=isValidBST(root,null,null);
return res;
}
boolean isValidBST(TreeNode root,TreeNode min,TreeNode max){
if(root==null) return true;
// 对于min值与root之间的关系:bst中root应该始终小于min的值
if(min!=null&&root.val<=min.val) return false;
// bst中root应该始终大于max的值,否则返回false
if(max!=null&&root.val>=max.val) return false;
return isValidBST(root.left,min,root)&&isValidBST(root.right,root,max);
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def isValidBST(root,small,big):
if not root:return True
if small and root.val<=small.val :return False
if big and root.val>=big.val:return False
return isValidBST(root.left,small,root) and isValidBST(root.right,root,big)
return isValidBST(root,None,None)