144. 二叉树的前序遍历:
链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,2,3]
递归算法:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root!=null){
res.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
}
return res;
}
}
94. 二叉树的中序遍历:
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
递归算法:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root!=null){
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
}
return res;
}
}
145. 二叉树的后序遍历:
链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
递归算法:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root!=null){
postorderTraversal(root.left);
postorderTraversal(root.right);
res.add(root.val);
}
return res;
}
}
100. 相同的树:
链接:https://leetcode-cn.com/problems/same-tree/
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 1:
输入: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
输出: true
代码:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q== null){
return true;
}
if(p==null || q==null){
return false;
}
if(p.val!=q.val){//根不相同
return false;
}
return
isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
572. 另一个树的子树:
链接: https://leetcode-cn.com/problems/subtree-of-another-tree/
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
示例 1:
给定的树 s:
3
/ \
4 5
/ \
1 2
给定的树 t:
4
/ \
1 2
返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。
示例 2:
给定的树 s:
3
/ \
4 5
/ \
1 2
/
0
给定的树 t:
4
/ \
1 2
返回 false。
代码:
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s==null)
{
return false;
}
if(t==null){ //子树为空
return true;
}
if(s.val==t.val && isSameTree(s,t)){
return true;//两棵树相同
}
return isSubtree(s.left,t)||isSubtree(s.right,t);
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q== null){
return true;
}
if(p==null || q==null){
return false;
}
if(p.val!=q.val){
return false;
}
return
isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
104. 二叉树的最大深度:
链接: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
代码:
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int a = maxDepth(root.left);
int b = maxDepth(root.right);
return Math.max(a,b) + 1;
}
}
110. 平衡二叉树:
链接: https://leetcode-cn.com/problems/balanced-binary-tree/
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
返回 true 。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
返回 false 。
代码:
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
int a = maxDepth(root.left);
int b = maxDepth(root.right);
if(Math.abs(a-b)>1){
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
}
}