首先了解二叉树的3种遍历
1.
给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
class Solution {
public List<Integer> l = new ArrayList<Integer>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root == null)
return l;
getList(root);
return l;
}
public void getList(TreeNode root) {
l.add(root.val);
if(root.left == null && root.right != null) {
getList(root.right);
}else if(root.right == null && root.left!=null) {
getList(root.left);
}else if(root.left!=null&&root.right!=null){
getList(root.left);
getList(root.right);
}else
return;
}
}
2.
给定一个二叉树的根节点 root
,返回 它的 中序 遍历 。
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
inder(list,root);
return list;
}
public void inder(List<Integer> list,TreeNode root) {
if(root == null)
return;
inder(list,root.left);
list.add(root.val);
inder(list,root.right);
}
}
3.
给你一棵二叉树的根节点 root
,返回其节点值的 后序遍历 。
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
getMax(root,res);
return res;
}
public void getMax(TreeNode root,List res) {
if(root==null)
return;
getMax(root.left,res);
getMax(root.right,res);
res.add(root.val);
}
}
4.
给你两棵二叉树的根节点 p
和 q
,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q==null){
return true;
}
if(p!=null && q!=null && q.val==p.val ){
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}else {
return false;
}
}
}
5.
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
return isS(root.left,root.right);
}
public boolean isS(TreeNode r1,TreeNode r2) {
if(r1 == null && r2 == null) {
return true;
}
if(r1 == null || r2 == null || r1.val != r2.val) {
return false;
}
return isS(r1.left,r2.right) && isS(r1.right,r2.left);
}
}
6.
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
return Math.max(getMax(root.left,1),getMax(root.right,1));
}
public int getMax(TreeNode root,int count) {
if(root == null) {
return count;
}
return Math.max(getMax(root.left,count+1),getMax(root.right,count+1));
}
}
7.
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return getTree(nums,0,nums.length - 1);
}
public TreeNode getTree(int[] nums,int left,int right) {
if(left > right)
return null;
int mid = (left + right) / 2;
TreeNode tree = new TreeNode(nums[mid]);
tree.left = getTree(nums,left,mid - 1);
tree.right = getTree(nums,mid + 1,right);
return tree;
}
}
8.
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) {
return true;
}
return balanced(root) != -1;
}
public int balanced(TreeNode root) {
if(root == null)
return 0;
int leftH,rightH;
if((leftH = balanced(root.left)) == -1 || (rightH = balanced(root.right)) == -1 || Math.abs(leftH - rightH) > 1)
return -1;
return Math.max(leftH,rightH) + 1;
}
}
9.
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
class Solution {
public int minDepth(TreeNode root) {
if(root == null)
return 0;
if(root.left == null && root.right != null)
return 1+minDepth(root.right);
if(root.right == null && root.left != null)
return 1+minDepth(root.left);
return Math.min(minDepth(root.right),minDepth(root.left)) + 1;
}
}
10.
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
class Solution {
public boolean is = false;
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null)
return false;
dfs(root,root.val,targetSum);
return is;
}
public void dfs(TreeNode root,int sum,int goal) {
if(root.left == null && root.right == null) {
if(sum == goal)
is = true;
return;
}
if(root.left != null)
dfs(root.left,sum + root.left.val,goal);
if(root.right != null)
dfs(root.right,sum + root.right.val,goal);
}
}
11.
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)
return null;
TreeNode r1 = root.right;
root.right = invertTree(root.left);
root.left = invertTree(r1);
return root;
}
}
12.
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode r1 = root;
while(true) {
if(q.val < r1.val && p.val < r1.val) {
r1 = r1.left;
}else if(q.val > r1.val && p.val > r1.val) {
r1 = r1.right;
}else
break;
}
return r1;
}
}
13.
class Solution {
public List<String> res = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
dfs("",root);
return res;
}
public void dfs(String sum,TreeNode root) {
if(root == null)
return;
if(root.left == null && root.right == null) {
sum += String.valueOf(root.val);
res.add(sum);
}
sum += String.valueOf(root.val)+"->";
dfs(sum,root.left);
dfs(sum,root.right);
}
}
14.
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root == null)
return 0;
int res = 0;
if(root.left != null && root.left.left == null && root.left.right == null) {
res += root.left.val;
}
return sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right)+res;
}
}