二叉树的中序遍历

力扣题目链接
给定一个二叉树的根节点 root,返回它的中序遍历。

  • 递归,写个新方法来递归
/**
* 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>();
inorder(root, res);
return res;
}
public void inorder(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
}

二叉树的最大深度

力扣题目链接
给定一个二叉树 root,返回其最大深度。二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数。

  • 简单的二叉树题目,递归是最好的选择
/**
* 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;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}

翻转二叉树*

力扣题目链接
给你一棵二叉树的根节点 root,翻转这棵二叉树,并返回其根节点。

  • 递归
/**
* 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 TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
TreeNode temp = root.right;
root.right = root.left;
root.left = temp;
invertTree(root.left);
invertTree(root.right);
return root;
}
}

对称二叉树

力扣题目链接
给你一个二叉树的根节点 root,检查它是否轴对称。

  • 递归,设计一个新函数进行递归
/**
* 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) {
return root == null || dfs(root.left, root.right);
}
private boolean dfs(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null || left.val != right.val) {
return false;
}
return dfs(left.left, right.right) && dfs(left.right, right.left);
}
}

二叉树的直径

力扣题目链接
给你一棵二叉树的根节点,返回该树的直径。二叉树的直径是指树中任意两个节点之间最长路径的长度。这条路径可能经过也可能不经过根节点 root。两节点之间路径的长度由它们之间边数表示。

  • 递归每个节点产生的最长链
  • 从根节点开始,依次计算最长答案(当前答案,与左右子树最长链之和)
  • 最长链在左右子树最长链之间产生
/**
* 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 {
private int ans;
public int diameterOfBinaryTree(TreeNode root) {
dfs(root);
return ans;
}
private int dfs(TreeNode root) {
if (root == null) {
return -1;
}
int maxLeft = dfs(root.left) + 1;
int maxRight = dfs(root.right) + 1;
ans = Math.max(ans, maxLeft + maxRight);
return Math.max(maxLeft, maxRight);
}
}

验证二叉搜索树

力扣题目链接
给你一个二叉树的根节点 root,判断其是否是一个有效的二叉搜索树。

  • 中序遍历存结果,判断是否是严格的递增序列
class Solution {
List<Integer> res = new ArrayList<>();
public boolean isValidBST(TreeNode root) {
if (root == null)
return true;
inOrder(root);
for (int i = 1; i < res.size(); i++) {
if (res.get(i) <= res.get(i - 1)) {
return false;
}
}
return true;
}
private void inOrder(TreeNode root) {
if (root != null) {
inOrder(root.left);
res.add(root.val);
inOrder(root.right);
}
}
}