94. 二叉树的中序遍历
方法一:递归法
/**
* 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 List<Integer> inorderTraversal(TreeNode root) {
List<Integer> arraylist=new ArrayList<>();
if(root==null){
return arraylist;
}
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode current=root;
while(current!=null||!stack.isEmpty()){
while(current!=null){
stack.push(current);
current=current.left;
}
current=stack.pop();
arraylist.add(current.val);
current=current.right;
}
return arraylist;
}
}
方法二:迭代法
/**
* 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 List<Integer> inorderTraversal(TreeNode root) {
// 中序遍历
List<Integer> res = new ArrayList<Integer>();
inorderTraversal(root, res);
return res;
}
public void inorderTraversal(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}
inorderTraversal(node.left, res);
res.add(node.val);
inorderTraversal(node.right, res);
}
}
104. 二叉树的最大深度
方法一:递归方法
/**
* 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 int maxDepth(TreeNode root) {
if(root==null){
return 0;
}else {
int leftLength = maxDepth(root.left)+1;
int rightLength = maxDepth(root.right)+1;
return Math.max(leftLength,rightLength);
}
}
}
方法二:层序遍历
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int ans = 0;
int size = 0;
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
size = queue.size();
while (size > 0) {
root = queue.poll();
if (root.left != null) {
queue.offer(root.left);
}
if (root.right != null) {
queue.offer(root.right);
}
size--;
}
ans++;
}
return ans;
}
}
226. 翻转二叉树
方法一:递归
/**
* 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 int maxDepth(TreeNode root) {
if(root==null){
return 0;
}else {
int leftLength = maxDepth(root.left)+1;
int rightLength = maxDepth(root.right)+1;
return Math.max(leftLength,rightLength);
}
}
}
方法二:迭代
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode node = root;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(node);
int size = 0;
while (!queue.isEmpty()) {
size = queue.size();
while (size > 0) {
TreeNode temp = queue.poll();
TreeNode left = temp.left;
if (left != null) {
queue.offer(left);
}
TreeNode right = temp.right;
if (right != null) {
queue.offer(right);
}
temp.left = right;
temp.right = left;
size--;
}
}
return root;
}
}
101. 对称二叉树
方法一:递归
/**
* 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 isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode left,TreeNode right) {
if(left==null&&right==null){
return true;
}
if(left==null|| right==null){
return false;
}
if(left.val!=right.val){
return false;
}
return isSymmetric(left.left,right.right)&& isSymmetric(left.right,right.left);
}
}
方法二:迭代
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode left, TreeNode right) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(left);
queue.offer(right);
while (!queue.isEmpty()) {
left = queue.poll();
right = queue.poll();
if (left == null && right == null) {
continue;
}
if (left == null || right == null || left.val != right.val) {
return false;
}
queue.offer(left.left);
queue.offer(right.right);
queue.offer(left.right);
queue.offer(right.left);
}
return true;
}
}
543. 二叉树的直径
方法一:递归
class Solution {
int ans = 0;
public int diameterOfBinaryTree(TreeNode root) {
diameterOfBinaryTreePLus(root);
return ans;
}
public int diameterOfBinaryTreePLus(TreeNode root) {
if(root==null){
return 0;
}
int left = diameterOfBinaryTreePLus(root.left);
int right = diameterOfBinaryTreePLus(root.right);
ans = Math.max(ans,left+right);
return Math.max(left+1,right+1);
}
}
102. 二叉树的层序遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (root == null) {
return result;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int size = 0;
while (!queue.isEmpty()) {
size = queue.size();
List<Integer> list = new ArrayList<Integer>();
while (size > 0) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
size--;
if (size == 0) {
result.add(list);
}
}
}
return result;
}
}
108. 将有序数组转换为二叉搜索树
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums,0,nums.length-1);
}
public TreeNode sortedArrayToBST(int[] nums,int left,int right){
if(left>right){
return null;
}
int mid = (left+right)/2;
TreeNode root = new TreeNode(nums[mid]);
root.left = sortedArrayToBST(nums,left,mid-1);
root.right = sortedArrayToBST(nums,mid+1,right);
return root;
}
}
98. 验证二叉搜索树
方法一:递归
初始错误写法
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
if (root.left != null) {
if (root.left.val >= root.val) {
return false;
}
}
if (root.right != null) {
if (root.right.val <= root.val) {
return false;
}
}
boolean left = isValidBST(root.left);
boolean right = isValidBST(root.right);
return left && right;
}
}
正确写法
/**
* Definition for a binary tree node.
* public class TreeNode {
* long val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(long val) { this.val = val; }
* TreeNode(long val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
return isValidBSTPlus(root,Long.MIN_VALUE,Long.MAX_VALUE);
}
public boolean isValidBSTPlus(TreeNode root,long min,long max) {
if (root == null) {
return true;
}
if (root.left != null) {
if (root.left.val >= root.val) {
return false;
}
}
if (root.right != null) {
if (root.right.val <= root.val) {
return false;
}
}
if(root.val>=max || root.val<=min){
return false;
}
boolean left = isValidBSTPlus(root.left,min,root.val);
boolean right = isValidBSTPlus(root.right,root.val,max);
return left && right;
}
}
方法二:中序遍历
/**
* 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 isValidBST(TreeNode root) {
long limit = Long.MIN_VALUE;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode currentNode = root;
while (currentNode != null || !stack.isEmpty()) {
while (currentNode != null) {
stack.push(currentNode);
currentNode = currentNode.left;
}
currentNode = stack.pop();
if (currentNode.val <= limit) {
return false;
}
limit = currentNode.val;
currentNode = currentNode.right;
}
return true;
}
}
230. 二叉搜索树中第K小的元素
方法一:排序
class Solution {
List<Integer> list = new ArrayList<>();
public int kthSmallest(TreeNode root, int k) {
sort(root);
return list.get(k-1);
}
void sort(TreeNode root){
if(root==null){
return;
}
sort(root.left);
list.add(root.val);
sort(root.right);
}
}
方法二:迭代
/**
* 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 int kthSmallest(TreeNode root, int k) {
TreeNode curNode = root;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (!stack.isEmpty()||curNode!=null) {
while (curNode != null) {
stack.push(curNode);
curNode = curNode.left;
}
curNode = stack.pop();
k--;
if (k == 0) {
return curNode.val;
}
curNode = curNode.right;
}
return curNode.val;
}
}
199. 二叉树的右视图
方法一:层序遍历
/**
* 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 List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
if (root == null) {
return result;
}
TreeNode curNode = root;
// 层序遍历
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(curNode);
int size = 0;
while (!queue.isEmpty()) {
size = queue.size();
while (size > 0) {
curNode = queue.poll();
if (curNode.left != null) {
queue.offer(curNode.left);
}
if (curNode.right != null) {
queue.offer(curNode.right);
}
size--;
if (size == 0) {
result.add(curNode.val);
}
}
}
return result;
}
}
方法二:深度遍历 (根 右 左)
/**
* 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 {
List<Integer> result = new ArrayList<Integer>();
public List<Integer> rightSideView(TreeNode root) {
sort(root, 0);
return result;
}
public void sort(TreeNode root, Integer length) {
if (root == null) {
return;
}
if (length == result.size()) {
result.add(root.val);
}
length++;
sort(root.right, length);
sort(root.left, length);
}
}
105. 从前序与中序遍历序列构造二叉树
方法一:递归方法
class Solution {
int[] preorder;
HashMap<Integer,Integer> dic = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.preorder = preorder;
for(int i=0;i<inorder.length;i++){
dic.put(inorder[i],i);
}
return recur(0,0,inorder.length-1);
}
TreeNode recur(int root,int left,int right){
if(left>right){
return null;
}
TreeNode node = new TreeNode(preorder[root]);
int i = dic.get(preorder[root]);
node.left = recur(root+1,left,i-1);
node.right = recur(root+i-left+1,i+1,right);
return node;
}
}
方法二:迭代方法
暂时放弃
437. 路径总和 III
方法一:常规迭代
/**
* 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 int pathSum(TreeNode root, int targetSum) {
if(root==null){
return 0;
}
int ret = rootSum(root,targetSum);
ret += pathSum(root.left,targetSum);
ret += pathSum(root.right,targetSum);
return ret;
}
public int rootSum(TreeNode root, long targetSum) {
int ret = 0;
if (root == null) {
return 0;
}
int val = root.val;
if (val == targetSum) {
ret++;
}
ret += rootSum(root.left, targetSum - val);
ret += rootSum(root.right, targetSum - val);
return ret;
}
}
方法二:前缀法
/**
* 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 {
Map<Long, Integer> prefixMap;
int target;
public int pathSum(TreeNode root, int targetSum) {
prefixMap = new HashMap<>();
target = targetSum;
prefixMap.put(0l, 1);
return recur(root, 0l);
}
private int recur(TreeNode node, Long curSum) {
if (node == null) {
return 0;
}
int res = 0;
curSum += node.val;
res += prefixMap.getOrDefault(curSum - target, 0);
prefixMap.put(curSum, prefixMap.getOrDefault(curSum, 0) + 1);
int left = recur(node.left, curSum);
int right = recur(node.right, curSum);
res = res + left + right;
prefixMap.put(curSum, prefixMap.get(curSum) - 1);
return res;
}
}
236. 二叉树的最近公共祖先
方法:
核心思路:
/**
* 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&&right!=null){
return root;
}
if(left!=null){
return left;
}
if(right!=null){
return right;
}
return null;
}
}